【问题标题】:Normalized to 100 data within groups归一化为组内的 100 个数据
【发布时间】:2015-12-13 11:09:33
【问题描述】:

我有一个如下所示的数据集:

Var Trait   Value
0   Trait1  42.26
1   Trait1  41.81
2   Trait1  41.21
0   Trait2  47.82
1   Trait2  51.44
2   Trait2  51.42
0   Trait3  10.27
1   Trait3  10.63
2   Trait3  10.14

我想为每个特征和 var1 添加一个标准化为 100 个数据的第四列:

(42.26/42.26)*100=100

(41.81/42.26)*100=98.93

(41.21/42.26)*100=97.51

对于特征 2 和 var1=1,值大于 var1=0 值,因此在这种情况下,第 5 行将按如下方式计算: (47.82/51.44)*100=92.94

【问题讨论】:

  • 或者只是with(df, ave(Value, Trait, FUN = function(x) x/max(x)*100))
  • 非常感谢您的精彩建议!

标签: r normalization


【解决方案1】:

如果你的数据是data.table:

data[, col4:= (Value/max(Value))*100, by= Trait]

【讨论】:

    【解决方案2】:

    使用 dplyr

    library(dplyr)
    
    df %>% 
      group_by(Trait) %>% 
      mutate(Result = Value/max(Value) * 100)
    
    # Output
    # Source: local data frame [9 x 4]
    # Groups: Trait [3]
    # 
    #     Var  Trait Value    Result
    #   (int) (fctr) (dbl)     (dbl)
    # 1     0 Trait1 42.26 100.00000
    # 2     1 Trait1 41.81  98.93516
    # 3     2 Trait1 41.21  97.51538
    # 4     0 Trait2 47.82  92.96267
    # 5     1 Trait2 51.44 100.00000
    # 6     2 Trait2 51.42  99.96112
    # 7     0 Trait3 10.27  96.61336
    # 8     1 Trait3 10.63 100.00000
    # 9     2 Trait3 10.14  95.39040
    

    【讨论】:

    • 变异是否创建新列?
    • @VasoGene 请接受/支持有助于解决您的问题的答案。
    【解决方案3】:

    或使用标准 R:

    dat$Normalized <- (dat$Value / ave(dat$Value, dat$Trait, FUN = max)) * 100
    > dat
      Var  Trait Value Normalized
    1   0 Trait1 42.26  100.00000
    2   1 Trait1 41.81   98.93516
    3   2 Trait1 41.21   97.51538
    4   0 Trait2 47.82   92.96267
    5   1 Trait2 51.44  100.00000
    6   2 Trait2 51.42   99.96112
    7   0 Trait3 10.27   96.61336
    8   1 Trait3 10.63  100.00000
    9   2 Trait3 10.14   95.39040
    

    【讨论】:

      猜你喜欢
      • 2021-11-26
      • 1970-01-01
      • 1970-01-01
      • 2014-05-04
      • 1970-01-01
      • 2016-02-13
      • 1970-01-01
      • 2019-10-27
      • 2016-10-07
      相关资源
      最近更新 更多