I have a list ( in my real problem a double list y[[1:24]][[1:15]] but I think the solution would be the same) m <- matrix(1:3, 3, 3) x <- list(m, m+3, m+6) and as I want to have the sum of elements I use Reduce(`+`, x) having as result> Reduce(`+`, x)[,1] [,2] [,3] [1,] 12 12 12 [2,] 15 15 15 [3,] 18 18 18 How can I take the sum of the list elements ignoring NA element For example if I have x[[1]][1]<-NA Then I want to have [,1] [,2] [,3] [1,] 11 12 12 [2,] 15 15 15 [3,] 18 18 18 instead of> Reduce(`+`, x)[,1] [,2] [,3] [1,] NA 12 12 [2,] 15 15 15 [3,] 18 18 18 Thanks in advance Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Summing-list-with-NA-elements-tp4608167.html Sent from the R help mailing list archive at Nabble.com.
one solution is to set NAs to 0, e.g.,
m <- matrix(1:3, 3, 3)
x <- list(m, m+3, m+6)
x[[1]][1] <- NA
x. <- lapply(x, function (x) {x[is.na(x)] <- 0; x} )
Reduce("+", x.)
I hope it helps.
Best,
Dimitris
On 5/4/2012 11:19 AM, Evgenia wrote:> I have a list ( in my real problem  a double list y[[1:24]][[1:15]] but I
> think the solution would be the same)
>
> m<- matrix(1:3, 3, 3)
> x<- list(m, m+3, m+6)
>   and as I want to have the sum of elements I use Reduce(`+`, x)
> having as result
>
>> Reduce(`+`, x)
>       [,1] [,2] [,3]
> [1,]   12   12   12
> [2,]   15   15   15
> [3,]   18   18   18
>
> How can I take the sum of the list elements ignoring NA element
> For example if I have
> x[[1]][1]<-NA
>
> Then I want to have
>       [,1] [,2] [,3]
> [1,]  11   12   12
> [2,]   15   15   15
> [3,]   18   18   18
>
> instead of
>
>> Reduce(`+`, x)
>       [,1] [,2] [,3]
> [1,]   NA   12   12
> [2,]   15   15   15
> [3,]   18   18   18
>
> Thanks in advance
>
> Evgenia
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Summing-list-with-NA-elements-tp4608167.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014
Web: http://www.erasmusmc.nl/biostatistiek/
Inelegant, but this is one way:
Reduce(function(e1, e2){e1[is.na(e1)] <- 0; e2[is.na(e2)] <- 0; (e1 +
e2)}, x)
I.e., set the NAs to 0 before adding in the reduce function.
Michael
On Fri, May 4, 2012 at 5:19 AM, Evgenia <evgts at aueb.gr>
wrote:> I have a list ( in my real problem ?a double list y[[1:24]][[1:15]] but I
> think the solution would be the same)
>
> m <- matrix(1:3, 3, 3)
> x <- list(m, m+3, m+6)
> ?and as I want to have the sum of elements I use Reduce(`+`, x)
> having as result
>
>> Reduce(`+`, x)
> ? ? [,1] [,2] [,3]
> [1,] ? 12 ? 12 ? 12
> [2,] ? 15 ? 15 ? 15
> [3,] ? 18 ? 18 ? 18
>
> How can I take the sum of the list elements ignoring NA element
> For example if I have
> x[[1]][1]<-NA
>
> Then I want to have
> ? ? [,1] [,2] [,3]
> [1,] ?11 ? 12 ? 12
> [2,] ? 15 ? 15 ? 15
> [3,] ? 18 ? 18 ? 18
>
> instead of
>
>> Reduce(`+`, x)
> ? ? [,1] [,2] [,3]
> [1,] ? NA ? 12 ? 12
> [2,] ? 15 ? 15 ? 15
> [3,] ? 18 ? 18 ? 18
>
> Thanks in advance
>
> Evgenia
>
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Summing-list-with-NA-elements-tp4608167.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
I agree with you. I used this "trick" to take the desired results but I posted wondering If there was any other solution. Thanks Evgenia -- View this message in context: http://r.789695.n4.nabble.com/Summing-list-with-NA-elements-tp4608167p4608955.html Sent from the R help mailing list archive at Nabble.com.