4

谁能告诉我为什么会收到此错误:
Error in as.integer(tm) : cannot coerce type 'S4' to vector of type 'integer
我一直在搜索互联网,但无法解决我的问题。

      library(Matrix)
     long <- file("C:\\New folder (5)\\inra.bin", "rb")
     A=readBin(long, integer(), size=2,n=67420*1, signed=F)
    ta<-t(A)
  lot <- file("C:\\New folder (5)\\lat.img", "rb")
    B=readBin(lot, integer(), size=2,n=67420*1, signed=F)
     tb<-t(B)
       wind <- file("C:\\Wind_WFD_200201.bin", "rb")
       C=readBin(wind, double(), size=4,n=67420*248, signed=TRUE)
        D<-matrix(C,nrow=248,ncol=67420)
         for(d in 1:31)
       {
        M <- Matrix(-9999, 360, 720)
         tm<-t(M)
       for(i in 1:67420)
       {
    tm[ta[i],tb[i]]= 10 * ((D[(d-1)*8+1,i] + D[(d-1)*8+2,i] +D[(d-1)*8+3,i] +D[(d- 1)*8+4,i] +D[(d-1)*8+5,i] +D[(d-1)*8+6,i] +D[(d-1)*8+7,i] +D[(d-1)*8+8,i] ) / 8)

 }###gooooooood
 to.write <- sprintf("C:\\Yar_%0d.bin", d)
 writeBin(as.integer(tm), size=2,to.write)

}

4

1 回答 1

2

看看如何在 R 中拆分和写入 S4 对象的文件。在您的情况下,它似乎Matrix返回了一个 S4 对象。尝试这个:

foo <- Matrix(10,10,10)
slotnames(foo) 

这可能会就您要从tm对象中提取的内容提出建议。
但你为什么首先使用Matrix?如果你只是使用base::matrix这个问题应该会消失。编辑:查看包的文档Matrix,很明显as.integer不支持。您可能必须先使用as(x,matrix)

于 2012-04-27T11:26:14.050 回答