问题
Say I want to locate the maximum values in one column based on the value of another (i.e. max by group). I found a number of helpful threads on how to do this (ex1 ex2). For example, using the plyr package,
ddply(data, .(x), summarise, max.score=max(y))
returns a list of the maximum values of y for each x.
However, what if I then wanted to replace all elements in x < max(y) with max(y) itself? (The specific application would be to recode all dates in a particular set with that set's end date.)
Thanks for any help!
EDIT: akrun's solution worked perfectly. I ended up with
data$y <- with(data, ave(y, x, FUN=function(f) max(f, na.rm=T)))
来源:https://stackoverflow.com/questions/40623429/replacing-column-values-with-maximum-by-group