Question

Can anyone tell me why I am getting this error:
Error in as.integer(tm) : cannot coerce type 'S4' to vector of type 'integer?
I have been searching the internet but couldn't solve my problem.

      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)

}

Was it helpful?

Solution

Take a look at How to split and write to a file for S4 object in R . In your case, it appears that Matrix returns an S4 object. Try this:

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

That may give a suggestion as to what you want to extract from your tm object.
But why are you using Matrix in the first place? If you just use base::matrix this problem should disappear. Edit: peeking at the documentation for package Matrix, it's clear that as.integer is not supported. You probably would have to use as(x,matrix) first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top