【问题标题】:Subtract minimum of the column based on other column根据其他列减去该列的最小值
【发布时间】:2014-07-27 09:47:00
【问题描述】:

我有一个如下的数据框:

 d

  year total  file
  1999        3931.12000 A
  2002        4273.71020 A
  2005        4601.41493 A
  2008        4101.32100 A
  1999         346.82000 B
  2002         134.30882 B
  2005         130.43038 B
  2008          88.27546 B

我希望由文件确定的每个组中的总数及其最小值的差异。
我可以考虑通过以下方式获得最低限度:

 tapply(d$total, d$file, min)

但我想不出明智的方法来获得减去最小值的向量。

【问题讨论】:

    标签: r dataframe tapply


    【解决方案1】:

    我建议withinave。像这样的:

    within(mydf, {
      tot2 <- ave(total, file, FUN = function(x) x - min(x))
    })
    #   year      total file      tot2
    # 1 1999 3931.12000    A   0.00000
    # 2 2002 4273.71020    A 342.59020
    # 3 2005 4601.41493    A 670.29493
    # 4 2008 4101.32100    A 170.20100
    # 5 1999  346.82000    B 258.54454
    # 6 2002  134.30882    B  46.03336
    # 7 2005  130.43038    B  42.15492
    # 8 2008   88.27546    B   0.00000
    

    或者,使用“data.table”:

    library(data.table)
    DT <- data.table(mydf)
    DT[, tot2 := total - min(total), by = file][]
    

    或者,使用“dplyr”:

    library(dplyr)
    mydf %>% group_by(file) %>% mutate(tot2 = total - min(total))
    

    【讨论】:

      【解决方案2】:

      使用tapply

       a1 <- tapply(d$total, d$file, min)
       d$total-a1[match(d$file, names(a1))]
       #     A         A         A         A         B         B         B         B 
      #  0.00000 342.59020 670.29493 170.20100 258.54454  46.03336  42.15492   0.00000 
      

      【讨论】: