Hi. I have a list where each object in the list has multiple parts. I'd like to take the mean of just one part of each object. Is it possible to do this with lapply? If not, can you recommend another function? Thanks. eric> x1 <- c(0,1,2,3) > x2 <- c(7,8) > x3 <- c(2,6,6,8) > x4 <- c(4,8) > > Lst1 <- list(label1 = x1,label2 = x2) > Lst2 <- list(label1 = x3, label2 = x4) > > BigList <- list(Lst1, Lst2) > > lapply(BigList$label1, mean)list()>[[alternative HTML version deleted]]
Hi Eric,> data<-list(x1 <- c(0,1,2,3),x2 <- c(7,8),x3 <- c(2,6,6,8), x4 <- c(4,8)) > lapply(X=data, mean)[[1]] [1] 1.5 [[2]] [1] 7.5 [[3]] [1] 5.5 [[4]] [1] 6 Hope it helps Chunhao eric lee-8 wrote:> > Hi. I have a list where each object in the list has multiple parts. I'd > like to take the mean of just one part of each object. Is it possible to > do > this with lapply? If not, can you recommend another function? Thanks. > > eric > >> x1 <- c(0,1,2,3) >> x2 <- c(7,8) >> x3 <- c(2,6,6,8) >> x4 <- c(4,8) >> >> Lst1 <- list(label1 = x1,label2 = x2) >> Lst2 <- list(label1 = x3, label2 = x4) >> >> BigList <- list(Lst1, Lst2) >> >> lapply(BigList$label1, mean) > list() >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. > >-- View this message in context: http://www.nabble.com/lapply-where-each-list-object-has-multiple-parts-tp19772755p19772797.html Sent from the R help mailing list archive at Nabble.com.
The function in 'lapply' gets each component, so write a function that does what you want given a component of the list: lapply(BigList, function(x) mean(x$label1)) Patrick Burns patrick at burns-stat.com +44 (0)20 8525 0696 http://www.burns-stat.com (home of S Poetry and "A Guide for the Unwilling S User") eric lee wrote:> Hi. I have a list where each object in the list has multiple parts. I'd > like to take the mean of just one part of each object. Is it possible to do > this with lapply? If not, can you recommend another function? Thanks. > > eric > > >> x1 <- c(0,1,2,3) >> x2 <- c(7,8) >> x3 <- c(2,6,6,8) >> x4 <- c(4,8) >> >> Lst1 <- list(label1 = x1,label2 = x2) >> Lst2 <- list(label1 = x3, label2 = x4) >> >> BigList <- list(Lst1, Lst2) >> >> lapply(BigList$label1, mean) >> > list() > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. > > >
Henrique Dallazuanna
2008-Oct-02 11:26 UTC
[R] lapply where each list object has multiple parts
Try this: rapply(BigList, mean)[c(1, 3)] On Wed, Oct 1, 2008 at 6:29 PM, eric lee <ericlee100 at gmail.com> wrote:> Hi. I have a list where each object in the list has multiple parts. I'd > like to take the mean of just one part of each object. Is it possible to do > this with lapply? If not, can you recommend another function? Thanks. > > eric > >> x1 <- c(0,1,2,3) >> x2 <- c(7,8) >> x3 <- c(2,6,6,8) >> x4 <- c(4,8) >> >> Lst1 <- list(label1 = x1,label2 = x2) >> Lst2 <- list(label1 = x3, label2 = x4) >> >> BigList <- list(Lst1, Lst2) >> >> lapply(BigList$label1, mean) > list() >> > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >-- Henrique Dallazuanna Curitiba-Paran?-Brasil 25? 25' 40" S 49? 16' 22" O