R中多个列表的元素明智均值 [英] Element wise mean of multiple lists in R

查看:62
本文介绍了R中多个列表的元素明智均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有十个庞大的列表(每个列表有七个元素,但元素很大),我需要计算这些列表的元素明智的均值.因此,如果有A1,A2,A3,...,A10列表.我需要计算:

I have ten huge lists(each list has seven element but elements are huge) and I need to calculate the element wise mean of these lists. So if there are A1, A2, A3,..., A10 lists. I need to calculate :

mean1 = mean(A1[[1]], A2[[1]], A3[[1]], ...,A10[[1]])
.
.
.
mean7 = mean(A1[[7]], A2[[7]], A3[[7]], ....A10[[7]])

我已经使用for循环完成了此操作,但是我想知道R是否提供了更好的解决方案. 预先谢谢你.

I have done it with for loop but I wanted to know if there is a better solution provided by R. Thank you in advance.

推荐答案

假设您的A是向量列表:

Anames <- paste0("A", 1:10)

# for completeness
for(A in Anames)
    assign(A, lapply(1:7, function(x) rnorm(1000)))

sapply(1:7, function(i)
{
    m <- sapply(Anames, function(A) get(A)[[i]])
    mean(m)
})

这避免了在内存中建立所有A的副本,而是一次检索一个副本并提取所需的向量.但是,如果您有足够的内存来存储所有这些数据,那么您可能也可以负担得起一个副本.

This avoids building a copy of all your As in memory, instead retrieving them one at a time and extracting the desired vector. But if you have enough memory to store all that data, you could probably afford to store a copy as well.

这篇关于R中多个列表的元素明智均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆