如何在R中的列表元素上应用均值函数 [英] How to apply mean function over elements of a list in R
本文介绍了如何在R中的列表元素上应用均值函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个列表,我想使用lapply()
来计算其元素的均值.例如,对于列表的第七项,我有:
I have a list and I want to use lapply()
to compute the mean of its elements. For example, for the seventh item of the list I have:
>list[[7]]
[1] 1 1 1 1 1 1 1 1 1 1
我的输出应该是:
> mean(temp[[7]][1:10])
[1] 1
但是当我像下面一样使用lapply()
时,结果将是其他.我该怎么办?
But when I use lapply()
like below the result would be something else. What should I do?
> lapply(list[[7]][1:10],mean)
[[1]]
[1] 1
[[2]]
[1] 1
.
.
.
[[10]]
[1] 1
推荐答案
要获取列表第7个元素的均值,只需使用mean(list[[7]])
.
要获取列表中每个元素的平均值,请使用lapply(list,mean)
.
将您的列表命名为list
是一个非常糟糕的主意.
To get the mean of the 7th element of the list just use mean(list[[7]])
.
To get the mean of each element of the list use lapply(list,mean)
.
And it's a really bad idea to call your list list
.
这篇关于如何在R中的列表元素上应用均值函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文