R_help Help
2009-Aug-12 02:05 UTC
[R] Smart way to sum a column in a matrix across all members in a list
Hi, I have a list that contains about 100 elements. Each element contains the following matrix structure a b 1 1 4 2 2 5 3 3 6 I'd like to sum column b across all elements. I'm thinking of using lapply or sapply but cannot get it to work. Any help would be really appreciated. Thank you. adschai
Henrique Dallazuanna
2009-Aug-12 02:10 UTC
[R] Smart way to sum a column in a matrix across all members in a list
Try this: sum(sapply(l, '[[', 'b')) On Tue, Aug 11, 2009 at 11:05 PM, R_help Help <rhelpacc@gmail.com> wrote:> Hi, > > I have a list that contains about 100 elements. Each element contains > the following matrix structure > > a b > 1 1 4 > 2 2 5 > 3 3 6 > > I'd like to sum column b across all elements. I'm thinking of using > lapply or sapply but cannot get it to work. Any help would be really > appreciated. Thank you. > > adschai > > ______________________________________________ > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]