简体   繁体   English

如何在R中逐元素列出求和

[英]How can I do list summation element-wise in R

this is very similar to 这非常类似于

Is there an R function for the element-wise summation of the matrices stored as elements in single list object? 是否有一个R函数,用于将元素存储在单个列表对象中的矩阵的逐元素求和?

How can I do list summation element-wise in R? 如何在R中逐元素列出求和?

For example, I want 例如,我想要

temp1<-list(c(rep(0,6)),c(1,2,3,4,5,6))
temp2<-list(c(2,3,4,5,6,7))

to yield 屈服

 3,5,7,9,11,13     

Just simple temp1[[2]] + temp2 doesn't work. 只是简单的temp1[[2]] + temp2不起作用。 Is there a special formula for it? 是否有特殊的公式?

我们可以使用Map

Map(`+`, temp1, temp2)[[2]]

Assuming that both the vectors in the list are of same length: 假设列表中的两个向量的长度相同:

lst=list(a=c(1,2,3,4,5,6),b=c(2,3,4,5,6,7))   
list(mapply(FUN = sum,lst$a,lst$b))'.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM