R:找到每一行中所有非零元素的方差 [英] R: Find the Variance of all Non-Zero Elements in Each Row
问题描述
我有一个像这样的数据框d:
I have a dataframe d like this:
ID Value1 Value2 Value3
1 20 25 0
2 2 0 0
3 15 32 16
4 0 0 0
我想做的是仅基于非零值来计算每个人(ID)的方差,并在不可能的情况下返回NA。
What I would like to do is calculate the variance for each person (ID), based only on non-zero values, and to return NA where this is not possible.
因此,例如,在此示例中,ID 1的方差为var(20,25),ID 2的
它将返回NA,因为您不能仅对一个条目计算方差,对于ID 3则为var将是var(15,32,16),对于ID 4,它将再次返回NULL,因为它根本没有数字可用来计算方差。
So for instance, in this example the variance for ID 1 would be var(20, 25), for ID 2 it would return NA because you can't calculate a variance on just one entry, for ID 3 the var would be var(15, 32, 16) and for ID 4 it would again return NULL because it has no numbers at all to calculate variance on.
我该怎么去对这个?我目前有以下(不完整的)代码,但这可能不是解决问题的最佳方法:
How would I go about this? I currently have the following (incomplete) code, but this might not be the best way to go about it:
len=nrow(d)
variances = numeric(len)
for (i in 1:len){
#get all nonzero values in ith row of data into a vector nonzerodat here
currentvar = var(nonzerodat)
Variances[i]=currentvar
}
这是一个玩具示例,但是我实际上正在使用的数据集具有40多个不同的值列来计算方差,因此可以轻松缩放的东西会很棒。
Note this is a toy example, but the dataset I'm actually working with has over 40 different columns of values to calculate variance on, so something that easily scales would be great.
推荐答案
Data <- data.frame(ID = 1:4, Value1=c(20,2,15,0), Value2=c(25,0,32,0), Value3=c(0,0,16,0))
var_nonzero <- function(x) var(x[!x == 0])
apply(Data[, -1], 1, var_nonzero)
[1] 12.5 NA 91.0 NA
这篇关于R:找到每一行中所有非零元素的方差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!