[R] vertical list sum

Duncan Mackay mackay at northnet.com.au
Sat Oct 30 01:37:33 CEST 2010


Hi Jorge

I tried your methods for all (which work for complete rows) and then I 
remove the first value of $y and repeated; both fail because of 
the  unequal numbers
The problem is when there are unequal numbers in the rows and trying to 
make a matrix of them.

I was trying some things with Greg's vaules.
x <- list()
x[[1]] <- c(9,5,7,2, 14,  4,  4,  3)
x[[2]] <- c(3,  6, 25,  2, 14,  3,  3 , 4)
x[[3]] <- c(28,  4 ,14,  3, 14,  2  ,4 , 5)
x[4] <- list(28 , 4 ,14 , 3, 14,  2 , 4  )

x.av <- list()
for(j in seq_along(1:max(sapply(x,length))) ) x.av[j] <- 
mean(sapply(x,"[",j),na.rm=T)
unlist(x.av) # if you want a vector

which Greg may have used first

Regards

Duncan

Duncan Mackay
Department of Agronomy and Soil Science
University of New England
ARMIDALE NSW 2351
Email home: mackay at northnet.com.au

At 08:53 30/10/2010, you wrote:
>Hi Greg,
>
>Here are two ways of doing it:
>
> > mylist <- list(x = rpois(10, 10), y = rpois(10, 20), z = rpois(10, 5))
> > mylist
>$x
>  [1]  3 13 14 16 10  7  3  5 12 14
>
>$y
>  [1] 17 16 26 13 23 24 16 28 23 12
>
>$z
>  [1]  2  6  5  5  5  1  9 11  6  4
>
> >
> > colMeans(do.call(rbind, mylist), na.rm = TRUE)
>  [1]  7.333333 11.666667 15.000000 11.333333 12.666667 10.666667  9.333333
>14.666667 13.666667
>[10] 10.000000
> >
> > Reduce("+", mylist)/length(mylist)
>  [1]  7.333333 11.666667 15.000000 11.333333 12.666667 10.666667  9.333333
>14.666667 13.666667
>[10] 10.000000
>
>
>HTH,
>Jorge
>
>
>On Fri, Oct 29, 2010 at 6:46 PM, Gregory Ryslik <> wrote:
>
> > Hi Everyone,
> >
> > I have a list of vectors like this (in this case it's 3 vectors but assume
> > the vector count and the length of each vector is not known):
> >
> > [[1]]
> > [1]  9  5  7  2 14  4  4  3
> >
> > [[2]]
> > [1]  3  6 25  2 14  3  3  4
> >
> > [[3]]
> > [1] 28  4 14  3 14  2  4  5
> >
> > What I want to do is take the average vertically. Thus I want to do 9+3+28
> > /3, 5+6+4 /3, etc... and then have it return a vector. I'm assuming that if
> > I can sum it, I can count it to so summing this would be just as helpful.
> >
> > I understand I can first go through each element of the list, get a vector,
> > cbind into  matrix and sum across but I was hoping to avoid that... I tried
> > getting it to work with mapply but am having difficulties...
> >
> > Thanks!
> >
> > Kind regards,
> > Greg
> > ______________________________________________
> > 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.
> >
>
>         [[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.



More information about the R-help mailing list