Hi Elk,

I tried to test with another case where coconut is in different position in
the sublist.
mango <- list( y=c(4,5,3,2,1,8), bw=c(4,18,12,3,4,9),
nn=paste0("a",1:6),data.frame(coconut=1:6))
banana <- list(data.frame(coconut=c(1,2,18,16,15)), y=c(4,5,9,2,1),
bw=c(4,18,22,3,4), nn=paste0("a",1:5))
pineapple <- list(data.frame(coconut=c(4,6,9)), y=c(8,24,12),
bw=c(14,31,36), nn=paste0("a",1:3)) 
list5<-list(mango,banana,pineapple)
#Your code
fun<-max # or min, median etc
sapply(lapply(list5,"[[",1),fun) 
[1]  8 18  9
# Here, first number should be 6, instead it did the max of y from mango.
#Then, I tried second solution of yours
sapply(lapply(lapply(list5,"[[",1),"[[","coconut"),fun) 
#Error in FUN(X[[1L]], ...) : subscript out of bounds
#Third solution
 sapply(lapply(list5,function(x)x[[1]][["coconut"]]),fun)
#Error in x[[1]][["coconut"]] : subscript out of bounds

#I tried with another way to come up with the result (not as elegant)
b3<-do.call("c",list5)
names(b3)[4]<-"mango"
 names(b3)[5]<-"banana"
 names(b3)[9]<-"pineapple"
 b4<-data.frame(key=names(b3),value=unlist(lapply(b3,FUN=function(x)
max(x))))
 key1<-c("mango","banana","pineapple")
 b5<-subset(b4, b4$key%in% key1)
 b5
#        key value
#4     mango     6
#5    banana    18
#9 pineapple     9
b6<-within(b5,{value<-as.numeric(as.character(value))})
max(b6$value)
#[1] 18
A.K.






______________________________________
If you reply to this email, your message will be added to the discussion below:
http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638926.html
This email was sent by arun kirshna (via Nabble)

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to