根据其他列减去列的最小值

时间:2014-07-27 09:47:01

标签: r dataframe tapply

我的数据框如下:

 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)

但是我想不出用减去最小值得到矢量的明智方法。

2 个答案:

答案 0 :(得分:4)

我建议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))

答案 1 :(得分: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