In R, how do I add a max by group? [duplicate]

If you redefine x first (the cbind makes both columns factors),

x<-data.frame(group,replicate)

you can use this:

merge(x,aggregate(replicate~group,x,FUN=max),all.x=TRUE,by="group")
   group replicate.x replicate.y
1      A           1           5
2      A           2           5
3      A           3           5
4      A           4           5
5      A           5           5
6      B           1           2
7      B           2           2
8      C           1           3
9      C           2           3
10     C           3           3

Leave a Comment