2

I have a dataframe as follows:

 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

I want to have difference of total and its minimum in each group determined by file.
I can think of getting the minimum with:

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

but I can't think of wise way to get the vector with subtracted minimums.

4

2 回答 2

4

I would suggest within and ave. Something like this:

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

Or, with "data.table":

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

Or, with "dplyr":

library(dplyr)
mydf %>% group_by(file) %>% mutate(tot2 = total - min(total))
于 2014-07-27T09:50:48.653 回答
2

Using the 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 
于 2014-07-27T10:04:49.220 回答