Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

data.frame: create column by applying a function to groups of rows

I have a data frame consisting of results from multiple runs of an experiment, each of which serves as a log, with its own ascending counter. I'd like to add another column to the data frame that has the maximum value of iteration for each distinct value of experiment.num in the sample below:

df <- data.frame(
     iteration = rep(1:5,5), 
     experiment.num = c(rep(1,5),rep(2,5),rep(3,5),rep(4,5),rep(5,5)), 
     some.val=42,
     another.val=12
)

In this example, the extra column would look like this (as all the subsets have the same maximum for iteration):

df$max <- rep(5,25)

The naive solution I currently use is:

df$max <- sapply(df$experiment.num,function(exp.num) max(df$iteration[df$experiment.num == exp.num]))

I've also used sapply(unique(df$experiment.num), function(n) c(n,max(df$iteration[df$experiment.num==n]))) to build another frame which I can then merge with the original, but both of these approaches seem more complicated than necessary.

The experiment.num column is a factor, so I think I might be able to exploit that to avoid iteratively doing this naive subsetting for all rows.

Is there a better way to get a column of maximum values for subsets of a data.frame?

like image 848
Mathew Hall Avatar asked Dec 16 '22 22:12

Mathew Hall


1 Answers

Using plyr:

ddply(df, .(experiment.num), transform, max = max(iteration))
like image 195
Julius Vainora Avatar answered Jan 30 '23 23:01

Julius Vainora