From: Peter Dalgaard BSA [mailto:p.dalgaard at biostat.ku.dk]
Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> writes:
Luis Silva wrote:
Dear helpers
I have a list where each element is a matrix (the list is obtained
with lapply). I want to sum those matrices. Is there a function to
do that? The sum function sums all the elements...
Here's a one-liner that converts your list into an array (by
unlisting it and then packing into an array with the right three
dimensions) and then runs apply(...,c(1,2),sum) to get the
answer you
want:
Didn't someone do an abind() function at some point? (Generalizing
cbind/rbind)
I believe there's an abind for Splus on StatLib, if I remember correctly by
Tony Plate & Rich Heiberger. Do not believe it was made available for R,
though I believe it'd be very useful.
[snipped]
However, aren't we ignoring the obvious?:
s<-0;(for(a in l)s<-s+a)
Indeed! (I guess somehow the evil of for loops in the old S in deeply
engrained in some of us.) Altough I like Sundar's version, too.
Cheers,
Andy
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, cont... {{dropped}}
From: Peter Dalgaard BSA [mailto:p.dalgaard at biostat.ku.dk]
Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> writes:
Luis Silva wrote:
Dear helpers
I have a list where each element is a matrix (the list is obtained
with lapply). I want to sum those matrices. Is there a function to
do that? The sum function sums all the elements...
Here's a one-liner that converts your list into an array (by
unlisting it and then packing into an array with the right three
dimensions) and then runs apply(...,c(1,2),sum) to get the
answer you
want:
Didn't someone do an abind() function at some point? (Generalizing
cbind/rbind)
I believe there's an abind for Splus on StatLib, if I remember correctly by
Tony Plate & Rich Heiberger. Do not believe it was made available for R,
though I believe it'd be very useful.
[snipped]
However, aren't we ignoring the obvious?:
s<-0;(for(a in l)s<-s+a)
Indeed! (I guess somehow the evil of for loops in the old S in deeply
engrained in some of us.) Altough I like Sundar's version, too.
Cheers,
Andy
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, cont... {{dropped}}
I've been meaning to submit the abind package to CRAN for a long time
now. I just did it.
Here's how one could do the matrix-list-sum using abind():
> library(abind)
> set.seed(1)
> m <- lapply(1:3,function(i) matrix(sample(10,8,rep=T),2,4))
> m
[[1]]
[,1] [,2] [,3] [,4]
[1,] 1 1 1 4
[2,] 6 7 1 7
[[2]]
[,1] [,2] [,3] [,4]
[1,] 7 10 1 7
[2,] 7 6 3 2
[[3]]
[,1] [,2] [,3] [,4]
[1,] 10 6 9 1
[2,] 3 4 2 2
> a <- do.call("abind", c(m, list(along=3)))
> apply(a, 1:2, sum)
[,1] [,2] [,3] [,4]
[1,] 18 17 11 12
[2,] 16 17 6 11
> m[[1]] + m[[2]] + m[[3]]
[,1] [,2] [,3] [,4]
[1,] 18 17 11 12
[2,] 16 17 6 11
> all(apply(a, 1:2, sum) == m[[1]] + m[[2]] + m[[3]])
[1] TRUE
>
-- Tony Plate
At Wednesday 07:58 PM 4/23/2003 -0400, Liaw, Andy wrote:
From: Peter Dalgaard BSA [mailto:p.dalgaard at biostat.ku.dk]
Barry Rowlingson <B.Rowlingson at lancaster.ac.uk> writes:
Luis Silva wrote:
Dear helpers
I have a list where each element is a matrix (the list is obtained
with lapply). I want to sum those matrices. Is there a function to
do that? The sum function sums all the elements...
Here's a one-liner that converts your list into an array (by
unlisting it and then packing into an array with the right three
dimensions) and then runs apply(...,c(1,2),sum) to get the
answer you
want:
Didn't someone do an abind() function at some point? (Generalizing
cbind/rbind)
I believe there's an abind for Splus on StatLib, if I remember correctly by
Tony Plate & Rich Heiberger. Do not believe it was made available for R,
though I believe it'd be very useful.
[snipped]
However, aren't we ignoring the obvious?:
s<-0;(for(a in l)s<-s+a)
Indeed! (I guess somehow the evil of for loops in the old S in deeply
engrained in some of us.) Altough I like Sundar's version, too.
Cheers,
Andy
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, cont...
{{dropped}}