Skip to content
Prev 302143 / 398503 Next

apply function over same column of all objects in a list

I still try to figure out, what you are finally asking for, but maybe

res<-sapply(list4,function(x)max(x[[1]]$coconut))
names(res)<-c("mango", "banana", "pineapple")
res
max(res)

is worth a try?

Especially i did not get the point of doing something like
x[which.max(x)] because this is in any case just max(x)

if you would like to plug in different functions or want to select
different columns,

fun<-max # or min, median etc
sapply(lapply(list4,"[[",1),fun)

could be a start, where lapply(list4,"[[",1) extracts the first object
in each sublist - which is in your case a data.frame with just one
column ('coconut'), where 'fun' can be applied to. So to address
'coconut' explicitly, just nest this lapply approach:

sapply(lapply(lapply(list4,"[[",1),"[[","coconut"),fun)
#or use an anonymous extractor function
sapply(lapply(list4,function(x)x[[1]][["coconut"]]),fun)

cheers

Am 02.08.2012 16:09, schrieb gail: