我是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
答案 0 :(得分:2)
您尝试在bigmemory对象中调用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对象。请注意,性能可能是 由于S4,降级(与应用常规R矩阵相比) 与从big.matrix对象中提取数据相关的开销。这个 某种限制是不可避免的,情况就是如此(甚至是 更糟糕的是与其他“自定义”数据结构。当然,这只会 如果你申请超长的行或者是非常重要的 列。
对于类似tapply的函数, bigtabulate 包也可能有帮助。 这个包的想法是分两步完成工作。
我们发现bigsplit接着是lapply或sapply可以 当分裂产生的子集是。时特别有效 合理的大小。
答案 1 :(得分:2)
好吧,我发现错误归功于之前的回答(#agstudy,我给你+1)... apply
方法来自base
包,如果我加载{ {1}}包,一切都像魅力......
biganalytics
再次感谢你!
答案 2 :(得分:0)
这个答案与原始问题有点偏离主题,而不是“为什么不apply(...)
工作?”,@ agstudy在上面回答,但“我如何获得{{1}的行方法对象?“我用Google搜索bigmemory
并最终到达:http://www.stat.yale.edu/~jay/HPCwR/examples/bioinfo/bioinfo3.txt
重新制作一个有趣的片段:
"r bigmemory rowmeans"
该示例继续展示如何使用并行方法(# 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
等)来计算行均值。