Replacing column values with maximum by group

风流意气都作罢 提交于 2020-01-25 22:18:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!