1

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)))
beddotcom
  • 447
  • 3
  • 11
  • 3
    Using `library(dplyr)`, try `data %>% group_by(x) %>% mutate(y=max(y))`. Also, `ddply` is from `plyr` not `dplyr`. – aichao Nov 16 '16 at 03:13
  • 3
    Or in base R, `data$y <- with(data, ave(y, x, FUN = max))` – akrun Nov 16 '16 at 03:22
  • I was having trouble with NAs in the data but realized I could pass a custom function FUN=function(x) max(x, na.rm=T) to replace them with the max as well. – beddotcom Nov 17 '16 at 04:54
  • 1
    I think @aichao should post their comment as an answer, since it works (and is what I would have suggested as well). – Kalin Dec 19 '17 at 00:31

0 Answers0