[英]Irreproducible example in big.matrix documentation (apply)
我是big.matrix
和相关软件包的big.matrix
,我尝试重现下面的例子```
Loading required package: stats
> Sys.setenv(LANG = "en")
> library(bigmemory)
Loading required package: bigmemory.sri
bigmemory >= 4.0 is a major revision since 3.1.2; please see package
biganalytics and http://www.bigmemory.org for more information.
> x <- big.matrix(5, 2, type="integer", init=0, dimnames=list(NULL, c("alpha", "beta")))
> x[,] <- round(rnorm(10))
Assignment will down cast from double to integer
Hint: To remove this warning type: options(bigmemory.typecast.warning=FALSE)
Mensajes de aviso perdidos
In SetAll.bm(x, value) :
> x
An object of class "big.matrix"
Slot "address":
<pointer: 0x22a1620>
> x[,]
alpha beta
[1,] -2 0
[2,] -1 0
[3,] 0 -1
[4,] 2 1
[5,] 0 0
> apply(x, 1, mean)
Error en as.vector(data) :
ningún método para coaccionar a esta clase S4 a un vector
从文档中,但最后一行给出了以下错误:
Error en as.vector(data) :
ningún método para coaccionar a esta clase S4 a un vector
最后一行说的是"there is no method for transform this S4 class to a vector"
你能指点一下吗?
我的R版本是
R.version
_
platform x86_64-unknown-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 2
minor 15.1
year 2012
month 06
day 22
svn rev 59600
language R
version.string R version 2.15.1 (2012-06-22)
nickname Roasted Marshmallows
您尝试在bigmemory对象中调用apply
。 后者没有隐式方法转换为矩阵(apply需要的参数)
apply(x, 1, mean)
Error in as.vector(data) :
no method for coercing this S4 class to a vector
强制转换为矩阵,纠正问题
apply(as.matrix(x), 1, mean)
[1] -1.5 -0.5 1.0 -0.5 -0.5
OP回答后编辑:
该包装biganalytics延伸bigmemory包与各种分析。 函数bigkmeans和binit也可以与本机R对象一起使用。 不过要小心:
申请big.matrix对象。 请注意,由于与从big.matrix对象中提取数据相关的S4开销,性能可能会降低(与应用常规R矩阵相比)。 这种限制是不可避免的,并且与其他“自定义”数据结构一样(甚至更糟)。 当然,如果您应用冗长的行或列,这只会具有重要意义。
对于类似tapply的函数, bigtabulate包也可能有用。 这个包的想法是分两步完成工作。
我们发现,当分裂产生的子集具有合理的大小时,bigsplit后面的lapply或sapply可能特别有效。
好吧,我发现错误归功于之前的回答(#agstudy,我给你+1)... apply
方法来自base
包,如果我加载biganalytics
包,一切都作为魅力......
> library(biganalytics)
> apply(x, 1, mean)
[1] 0.0 1.5 0.5 -1.0 0.5
再次感谢你!
这个答案与原始问题有点偏离主题,而不是“为什么不apply(...)
工作?”,@ agstudy在上面回答,但“我如何获得一个大bigmemory
对象的行方式?” 我用"r bigmemory rowmeans"
搜索了"r bigmemory rowmeans"
并最终在这里: http : //www.stat.yale.edu/~jay/HPCwR/examples/bioinfo/bioinfo3.txt
再现一个有趣的片段:
# Get the row means, three different ways.
system.time({
a <- rep(0, nrow(z))
for (i in 1:nrow(z)) {
a[i] <- mean(z[i,])
}
}) # Will definitely work on both matrix and big.matrix
# matrix timing: about 30 seconds
# big.matrix timing: about 270 seconds
# The price for using bigmemory with lots of very small
# operations is the overhead of S3/S4 dispatch.
system.time({
a <- apply(z, 1, mean)
}) # Works on a matrix only; interesting that it is slower.
# matrix timing: 45 seconds
system.time({
myfunc <- function(i) return(mean(z[i,]))
a <- sapply(1:nrow(z), myfunc)
}) # Will definitely work on both matrix and big.matrix
# matrix timing: About 40 seconds
# big.matrix timing: About 306 seconds
该示例继续展示如何使用并行方法( doMC
等)来计算行均值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.