在数据帧中成对计算有效观测值的数量(无NA) [英] Count the number of valid observations (no NA) pairwise in a data frame
问题描述
说我有一个像这样的数据框:
Say I have a data frame like this:
Df <- data.frame(
V1 = c(1,2,3,NA,5),
V2 = c(1,2,NA,4,5),
V3 = c(NA,2,NA,4,NA)
)
现在,我想计算两个变量的每种组合的有效观察数.为此,我编写了一个函数 sharedcount
:
Now I want to count the number of valid observations for every combination of two variables. For that, I wrote a function sharedcount
:
sharedcount <- function(x,...){
nx <- names(x)
alln <- combn(nx,2)
out <- apply(alln,2,
function(y)sum(complete.cases(x[y]))
)
data.frame(t(alln),out)
}
这给出了输出:
> sharedcount(Df)
X1 X2 out
1 V1 V2 3
2 V1 V3 1
3 V2 V3 2
很好,但是函数本身在大数据帧(600个变量和大约10000个观察值)上花费很长时间.我觉得我正在监督一种更简单的方法,尤其是因为cor(...,use ='pairwise')的运行速度要快得多,而它必须执行类似的操作:
All fine, but the function itself takes pretty long on big dataframes (600 variables and about 10000 observations). I have the feeling I'm overseeing an easier approach, especially since cor(...,use='pairwise') is running still a whole lot faster while it has to do something similar :
> require(rbenchmark)
> benchmark(sharedcount(TestDf),cor(TestDf,use='pairwise'),
+ columns=c('test','elapsed','relative'),
+ replications=1
+ )
test elapsed relative
2 cor(TestDf, use = "pairwise") 0.25 1.0
1 sharedcount(TestDf) 1.90 7.6
任何提示都值得赞赏.
注意:使用Vincent的技巧,我编写了一个返回相同数据帧的函数.在下面的答案中输入代码.
Note : Using Vincent's trick, I wrote a function that returns the same data frame. Code in my answer below.
推荐答案
以下内容会更快:
x <- !is.na(Df)
t(x) %*% x
# test elapsed relative
# cor(Df) 12.345 1.000000
# t(x) %*% x 20.736 1.679708
这篇关于在数据帧中成对计算有效观测值的数量(无NA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!