计算big.matrix的平均值

3
我正在使用bigmemorybiganalytics软件包,并尝试计算big.matrix对象的平均值。 biganalytics的文档(例如?biganalytics)建议mean()可用于big.matrix对象,但失败了。
x <- big.matrix(5, 2, type="integer", init=0, 
+   dimnames=list(NULL, c("alpha", "beta")))
x
# An object of class "big.matrix"
# Slot "address":
# <pointer: 0x00000000069a5200>
x[,1] <- 1:5
x[,]
#      alpha beta
# [1,]     1    0
# [2,]     2    0
# [3,]     3    0
# [4,]     4    0
# [5,]     5    0
mean(x)
# [1] NA
# Warning message:
# In mean.default(x) : argument is not numeric or logical: returning NA

虽然有些东西可以正常工作:

colmean(x)
# alpha  beta 
#     3     0 
sum(x)
# [1] 15
mean(x[])
# [1] 1.5
mean(colmean(x))
# [1] 1.5

没有使用mean()函数,使用mean(colmean(x))似乎是下一个最佳选择:
# try it on something bigger
x = big.matrix(nrow=10000, ncol=10000, type="integer")
x[] <- c(1:(10000*10000))
mean(colmean(x))
# [1] 5e+07
mean(x[])
# [1] 5e+07
system.time(mean(colmean(x)))
#    user  system elapsed 
#    0.19    0.00    0.19 
system.time(mean(x[]))
#   user  system elapsed 
#   0.28    0.11    0.39 

可以想象,对于具有大量列的矩形矩阵,mean() 可能仍然更快。

不知道为什么 mean() 对我不起作用,有什么想法吗?


1
你能提供你的 sessionInfo() 吗?我刚刚在 R 3.2.2 上安装了 bigmemorybiganalytics,没有出现任何问题。 - cdeterman
好的,重新安装 biganalytics 解决了问题。不确定出了什么问题。 - chriss
1个回答

0

好的 - 重新安装 biganalytics 看起来已经解决了这个问题。 现在我有:

library("biganalytics")
x = big.matrix(10000,10000, type="integer")
for(i in 1L:10000L) { j = c(1L:10000L) ; x[i,] <- i*10000L + j }
mean(x)
# [1] 50010001
mean(x[,])
# [1] 50010001
mean(colmean(x))
# [1] 50010001
system.time(replicate(100, mean(x)))
#   user  system elapsed 
#  20.16    0.02   20.23 
system.time(replicate(100, mean(colmean(x))))
#   user  system elapsed 
#  20.08    0.00   20.24 
system.time(replicate(100, mean(x[,])))
#   user  system elapsed 
#  31.62   12.88   44.74 

一切都好。我的sessionInfo()现在是:

R version 3.1.0 (2014-04-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C                            LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] biganalytics_1.1.12 biglm_0.9-1         DBI_0.3.1           foreach_1.4.2       bigmemory_4.5.8     bigmemory.sri_0.1.3

loaded via a namespace (and not attached):
[1] codetools_0.2-8 iterators_1.0.7 Rcpp_0.11.2    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接