r - big.matrix 文档中不可重现的示例(应用)

标签 r bigdata r-bigmemory

我是 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/

相关文章:

重复向量以填充数据框中的列

r - 如何重命名 R 中 DT 数据表的过滤器中的占位符?

r - 在 R 中合并多个栅格

r - 使用 big.matrix 对象计算欧几里得距离矩阵

r - bigmemory 和带有文件支持的 friend 的示例

r - DST 前后的 POSIXct 时间?

mysql - Oracle 11g 对比 MySQL 对比 Hadoop :- benchmarking for 10^6 to 10^9 records

python - 使用 Python 解析 12GB CSV

mysql - 如何在学说中插入非常引用

r - 计算 R 中大矩阵的零空间