[英]Summing elements of a list in r
我有一个包含 10 个元素的列表,每个元素包含两个变量。
$`1.10%.100`
sqm tempo
127.4444 0.1500
$`10.10%.100`
sqm tempo
367.00 0.16
$`2.10%.100`
sqm tempo
248.7778 0.1900
$`3.10%.100`
sqm tempo
139.00 0.13
$`4.10%.100`
sqm tempo
139.00 0.14
$`5.10%.100`
sqm tempo
223.00 0.17
$`6.10%.100`
sqm tempo
155.4444 0.1500
$`7.10%.100`
sqm tempo
248.7778 0.1400
$`8.10%.100`
sqm tempo
90.11111 0.14000
$`9.10%.100`
sqm tempo
90.11111 0.17000
我想两个两个地计算平均值,然后将其填入一个新列表中。
例如,我想要:mean(127.4444, 367.00) 和 mean(0.15, 0.16)...对于元素 3 和 4、5 和 6 等也是如此。
你可以试试:
#generate some sample data hopefully similar to yours
set.seed(1)
#set the size of the aggregation (you can change it to 10 for instance)
n<-2
mylist<-replicate(10,c(sqm=runif(1,100,1000),tempo=runif(1)),simplify=FALSE)
matrix(colMeans(`dim<-`(do.call(rbind,mylist),c(n,length(mylist)/n*2))),ncol=2)
# [,1] [,2]
#[1,] 477.2629 0.6401658
#[2,] 615.8607 0.7795937
#[3,] 475.7899 0.1191715
#[4,] 755.5889 0.4409015
#[5,] 593.9442 0.8846757
第一列是sqm
元素的平均值,另一列是tempo
平均值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.