R中的按元素均值 [英] Element-wise mean in R
本文介绍了R中的按元素均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在R中,我有两个向量:
In R, I have two vectors:
a <- c(1, 2, 3, 4)
b <- c(NA, 6, 7, 8)
如何找到两个向量的逐元素均值,不带循环地删除NA?即我想获得
How do I find the element-wise mean of the two vectors, removing NA, without a loop? i.e. I want to get the vector of
(1, 4, 5, 6)
我知道函数mean()
,我知道参数na.rm = 1
.但是我不知道如何把东西放在一起.可以肯定的是,实际上我有成千上万个带有NA的向量出现在不同的位置,因此任何依赖于维度的解决方案都行不通.谢谢.
I know the function mean()
, I know the argument na.rm = 1
. But I don't know how to put things together. To be sure, in reality I have thousands of vectors with NA appearing at various places, so any dimension-dependent solution wouldn't work. Thanks.
推荐答案
操作方法:
rowMeans(cbind(a, b), na.rm=TRUE)
或
colMeans(rbind(a, b), na.rm=TRUE)
这篇关于R中的按元素均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文