在所述数据帧(DF),我需要插入一个新列的每个组的S1中定义其最大价值。
df=data.frame(
Sl= c(1,1,2,2,2,2,3,3,4,4,4,5),
Value= c(0,2,1,3,5,2,5,8,10,3,5,7)
)
#Expected outcome:
df$col3=c(2,2,5,5,5,5,5,10,10,7,7,7)
我们可以max
按组使用
library(dplyr)
df %>%
group_by(ID) %>%
mutate(col3 = max(Value))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句