我是 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
最后一行说的是“没有将这个 S4 类转换为向量的方法”
你能给我指点一下吗?
我的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(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 也可以与 native R 对象。但要小心:
apply for big.matrix objects. Note that the performance may be degraded (compared to apply with regular R matrices) because of S4 overhead associated with extracting data from big.matrix objects. This sort of limitation is unavoidable and would be the case (or even worse) with other "custom" data structures. Of course, this would only be partically significant if you are applying over lengthy rows or columns.
对于类似 tapply 的函数,bigtabulate 包也可能有帮助。 这个包的想法是分两步完成这项工作。
We have found that bigsplit followed by lapply or sapply can be particularly effective, when the subsets produced by the split are of reasonable size.
关于r - big.matrix 文档中不可重现的示例(应用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14023795/