【问题标题】:Changing values in a column with values from another vector based on a condition根据条件使用来自另一个向量的值更改列中的值
【发布时间】:2019-10-27 10:01:20
【问题描述】:

想象一下我有这个 df。

library(dplyr)
library(scales)
df <- data.frame("item" = 1:10, "quantity" = c(21,15,12,14,26,12,13,15,24,26), "value" = c(90,110,105,102,86,112,75,84,34,99))

理想情况下,我希望将值最大化为 100。因此,我想获取任何值高于 95 的项目,然后将它们缩放到 1:5 的范围内。然后我想在 95 之上添加缩放值以获得 95 到 100 之间的值。这是我目前拥有的:

x <- df[df$value >= 95,]
x <- x$value
x <- rescale(x, to = c(0,5))
x  # [1] 4.23, 2.31, 1.15, 5.00, 0.00
df <- df %>% mutate(value = ifelse(value >= 95, 95 + x, value))

这给了我95+x[1],你可以想象到所有高于 95 的值。我想我需要一个 for 循环,但不太清楚如何构造它。谢谢。

期望的输出是:

Item Quantity Value
1       21    90 
2       15    99.23
3       12    97.31
4       14    96.15
5       26    86 
6       12    100
7       13    75 
8       15    84
9       24    34 
10      26    95

【问题讨论】:

  • 谢谢@N。威廉姆斯和@H 1,出于某种原因,我仍在进行统一更改。我确定这是我的问题,与您的回答无关,再次感谢。

标签: r dataframe dplyr data-manipulation


【解决方案1】:

这也有效:

x <- df[df$value >= 95,]
x <- x$value
x <- rescale(x, to = c(0,5))
x  # [1] 4.23, 2.31, 1.15, 5.00, 0.00
df$Value[df$Value>95] <- 95 + x

【讨论】:

    【解决方案2】:

    可以通过base r中的一个简单子集来实现:

    df <- data.frame("item" = 1:10, 
                     "quantity" = c(21,15,12,14,26,12,13,15,24,26),
                     "value" = c(90,110,105,102,86,112,75,84,34,99))
    
    df[df$value >= 95,"value"] <- 95 + scales::rescale(df[df$value >= 95,"value"], to=c(0, 5))
    
    df
    #>    item quantity     value
    #> 1     1       21  90.00000
    #> 2     2       15  99.23077
    #> 3     3       12  97.30769
    #> 4     4       14  96.15385
    #> 5     5       26  86.00000
    #> 6     6       12 100.00000
    #> 7     7       13  75.00000
    #> 8     8       15  84.00000
    #> 9     9       24  34.00000
    #> 10   10       26  95.00000
    

    reprex package (v0.3.0) 于 2019 年 6 月 12 日创建

    【讨论】:

      【解决方案3】:

      我不确定这是否是您正在寻找的,但使用 dplyr 和 scales 包:

      df %>% 
       mutate(Value = ifelse(Value >= 95, 95 + scales::rescale(Value, to = c(1, 5)), Value))
      

      编辑:

      df %>% 
        filter(Value >= 95) %>% 
        mutate(Value = 95 + scales::rescale(Value, to = c(0, 5))) %>% 
        bind_rows(filter(df, Value < 95)) %>% 
        arrange(Item)
      

      【讨论】:

      • 关闭,但这会再次创建统一的更改,而不是单独添加每个值的比例
      【解决方案4】:

      这是在 data.table 中,但我会这样做

      library(data.table)
      dt <- data.table("Item" = 1:10,
                       "Quantity" = c(21,15,12,14,26,12,13,15,24,26),
                       "Value" = c(90,110,105,102,86,112,75,84,34,99))
      
      dt[Value >= 95, Value := 95 + (Value - 95) / (max(Value) - 95) * 5]
      

      也在基地中

      df <- data.frame("Item" = 1:10,
                       "Quantity" = c(21,15,12,14,26,12,13,15,24,26),
                       "Value" = c(90,110,105,102,86,112,75,84,34,99))
      
      x <- df[df$Value >= 95, "Value"] - 95
      df[df$Value >= 95, "Value"] <- 95 + x / max(x) * 5
      

      【讨论】:

        【解决方案5】:

        不完全确定我是否掌握了预期的输出,但可能类似于以下内容:

        library(dplyr)
        
        df %>%
          group_by(grp = Value >= 95) %>%
          mutate(New_Value = ifelse(Value >= 95, 95 + ntile(Value, 5), Value))
        
        # A tibble: 10 x 5
        # Groups:   grp [2]
            Item Quantity Value grp   New_Value
           <int>    <dbl> <dbl> <lgl>     <dbl>
         1     1       21    90 FALSE        90
         2     2       15   110 TRUE         99
         3     3       12   105 TRUE         98
         4     4       14   102 TRUE         97
         5     5       26    86 FALSE        86
         6     6       12   112 TRUE        100
         7     7       13    75 FALSE        75
         8     8       15    84 FALSE        84
         9     9       24    34 FALSE        34
        10    10       26    99 TRUE         96
        

        或相同的方法,但使用scales::rescale()

        df %>%
          group_by(grp = Value >= 95) %>%
          mutate(New_Value = ifelse(Value >= 95, 95 + scales::rescale(Value, c(0,5)), Value))
        
        # A tibble: 10 x 5
        # Groups:   grp [2]
            Item Quantity Value grp   New_Value
           <int>    <dbl> <dbl> <lgl>     <dbl>
         1     1       21    90 FALSE      90  
         2     2       15   110 TRUE       99.2
         3     3       12   105 TRUE       97.3
         4     4       14   102 TRUE       96.2
         5     5       26    86 FALSE      86  
         6     6       12   112 TRUE      100  
         7     7       13    75 FALSE      75  
         8     8       15    84 FALSE      84  
         9     9       24    34 FALSE      34  
        10    10       26    99 TRUE       95  
        

        【讨论】:

          猜你喜欢
          • 2020-08-20
          • 1970-01-01
          • 2022-11-04
          • 1970-01-01
          • 1970-01-01
          • 2020-02-18
          • 1970-01-01
          • 2020-07-05
          • 2021-09-05
          相关资源
          最近更新 更多