Skip to content
Back to formatted view

Raw Message

Message-ID: <1343916561536-4638876.post@n4.nabble.com>
Date: 2012-08-02T14:09:21Z
From: gail
Subject: apply function over same column of all objects in a list
In-Reply-To: <1343913587745-4638873.post@n4.nabble.com>

Hi,

my dataset is the result of the function "density" on another set of data.
It refers to that data in its variable "call", though since all the results
are actually reproduced (except that I've removed all rows bar 10), I am not
sure why R still needs it. But I've understood now why your code doesn't
work on my data: your sublists are still just dataframes, whereas my
sublists contain all sorts of vectors (2 numerical vectors and 5 other
things). I've changed your data to make it look a bit more like mine, and
you'll see that the code doesn't work. 

mango <- list(data.frame(coconut=1:6), y=c(4,5,3,2,1,8),
bw=c(4,18,12,3,4,9), nn=paste0("a",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))
list4 <- list(mango, banana, pineapple)

b <- list()

 for(i in 1:3){
 b[[i]] <- list()
 b[[i]] <- lapply (list4[[i]]$coconut, FUN=function(x) x[which.max(x)])
 }

b
summary( b)

What I want to end up with is the following:

           coconut
mango       6
banana     18
pineapple   9
(that's for the individual sublists)

and 
           coconut
                18
(that's for the list as a whole).

Hope this is becoming a bit clearer ...



--
View this message in context: http://r.789695.n4.nabble.com/apply-function-over-same-column-of-all-objects-in-a-list-tp4638681p4638876.html
Sent from the R help mailing list archive at Nabble.com.