3

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

3 回答 3

2

好吧,由于先前的答案,我发现了错误(#agstudy,我给你一个+1)...该 apply方法来自base包,如果我加载biganalytics包,一切都像魅力一样...

> library(biganalytics)
> apply(x, 1, mean)
[1]  0.0  1.5  0.5 -1.0  0.5

再次感谢你!

于 2012-12-24T17:18:23.817 回答
2

您尝试调用apply一个 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 对象。请注意,由于与从 big.matrix 对象中提取数据相关的 S4 开销,性能可能会降低(与使用常规 R 矩阵相比)。这种限制是不可避免的,其他“自定义”数据结构也会出现这种情况(甚至更糟)。当然,这仅在您应用过长的行或列时才特别重要。

对于类似 tapply 的功能,bigtabulate 包也可能会有所帮助。这个包的想法是分两步完成这项工作。

我们发现,当拆分产生的子集大小合理时,bigsplit 后跟 lapply 或 sapply 可能特别有效。

于 2012-12-24T17:10:22.030 回答
0

这个答案与原始问题略有偏离,不是“为什么不起作用apply(...)?”,@agstudy 在上面回答了,而是“我如何获得bigmemory对象的行均值?” 我用谷歌搜索"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等)来计算行均值。

于 2012-12-24T17:17:22.110 回答