Monday, 9 September 2013

equivalent way using apply family instead of a for loop to get max value of each row in list of sublists R

equivalent way using apply family instead of a for loop to get max value
of each row in list of sublists R

What would be the equivalent to this using apply family functions or a
compbination between do.call and apply? I would like to keep it simple and
when possible in one line:
a <- list( as.data.frame(matrix(rnorm(12),4,3)),
as.data.frame(matrix(rnorm(12),4,3)),
as.data.frame(matrix(rnorm(12),4,3))
)
l <- list()
for (i in 1:length(a)) {
l[[i]] <- apply(a[[i]],1,max)
}
b <- do.call(data.frame, l)

No comments:

Post a Comment