r语言 - 为什么不能将类型"S4"强制为"整数"类型的向量?



谁能告诉我为什么我会收到此错误:
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)

}

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

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

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

最新更新