简体   繁体   中英

subtract first value from each subset of dataframe

I want to subtract the smallest value in each subset of a data frame from each value in that subset ie

A <- c(1,3,5,6,4,5,6,7,10)
B <- rep(1:4, length.out=length(A))
df <- data.frame(A, B)
df <- df[order(B),]

Subtracting would give me:

  A B
1 0 1
2 3 1
3 9 1
4 0 2
5 2 2
6 0 3
7 1 3
8 0 4
9 1 4

I think the output you show is not correct. In any case, from what you explain, I think this is what you want. This uses ave base function:

within(df, { A <- ave(A, B, FUN=function(x) x-min(x))})
  A B
1 0 1
5 3 1
9 9 1
2 0 2
6 2 2
3 0 3
7 1 3
4 0 4
8 1 4

Of course there are other alternatives such as plyr and data.table .

Echoing Arun's comment above, I think your expected output might be off. In any event, you should be able to use can use tapply to calculate subsets and then use match to line those subsets up with the original values:

subs <- tapply(df$A, df$B, min)

df$A <- df$A - subs[match(df$B, names(subs))]

df
  A B
1 0 1
5 3 1
9 9 1
2 0 2
6 2 2
3 0 3
7 1 3
4 0 4
8 1 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM