[R] number of items to replace is not a multiple of replacement length

ascotilla mmartincal at gmail.com
Fri Jun 29 13:25:41 CEST 2012


Hello,

I'm a complete newbie to R so sorry if this is too basic..:-S

I have to modify some scripts someone else did to make it work with my data.
For some reason, one of the scripts which were supposed to work is not, and
I get the error message "number of items to replace is not a multiple of
replacement length".

The script is this one:

*open_lpj_nc_gpp <- function(file_name,varname,y1850_file_no,
    recentre,startyr,nyrs)
  {
    library(ncdf)
    
    start_yr=y1850_file_no+(startyr-1850)

    print("************************************")
    filename=paste(file_name,start_yr,".nc",sep="")
    print("Opening:")
    print(varname)
    print("in:")
    print(filename)
    ncfile=open.ncdf(filename)
    mod1=get.var.ncdf(ncfile,varname)
    print("------------------------------------")
    
    a=dim(mod1)
    print(a)
    b=length(a)
    
    if (b==3) {
      mod =array(0,dim=c(a[1],a[2],a[3],nyrs))
      modi=array(0,dim=a)
    } else {
      mod =array(0,dim=c(a[1],a[2],1,nyrs))
      modi=array(0,dim=c(a[1],a[2],1))
      a[3]=1
    }
    if (recentre) {
        yay=mod[1:(a[1]/2),,,1]
        wow=mod1[(1+a[1]/2):a[1],,,]
        mod[1:(a[1]/2),,,1]=mod1[(1+a[1]/2):a[1],,,]
        mod[(1+a[1]/2):a[1],,,1]=mod1[1:(a[1]/2),,]
      } else {
        mod[,,,1]=mod1
      }
    remove(mod1)
    
    p=1
    while (p<nyrs) {
      y=p+start_yr
      p=p+1      
      remove(filename)
      filename=paste(file_name,y,".nc",sep="")
      
      print("Opening:")
      print(varname)
      print("in:")
      print(filename)
      print("------------------------------------")
      
      ncfile=open.ncdf(filename)
      modi[,,]=get.var.ncdf(ncfile,varname)
      if (recentre) {
        mod[1:(a[1]/2),,,p]=modi[(1+a[1]/2):a[1],,]
        mod[(1+a[1]/2):a[1],,,p]=modi[1:(a[1]/2),,]
      } else {
        mod[,,,p]=modi
      }
    }

    for (i in 1:a[1]) {
      for (j in 1:a[2]) {
        for (k in 1:a[3]) {
          for (l in 1:p)    {
            if (is.na(mod[i,j,k,l])==0) {
              if (mod[i,j,k,l]>1E9) {
                mod[i,j,k,l]=NaN
              }
            }
          }
        }
      }
    }
       
    print("************************************")
    return(mod)
    
  }
*

And this is the error I get:

*Error in mod[1:(a[1]/2), , , 1] = mod1[(1 + a[1]/2):a[1], , , ] : 
  number of items to replace is not a multiple of replacement length*

The dimensions of the mod1 file are [720 360 9 12]. I've tried to modify the
number of commas, and it worked to sort out a dimensions problem I had
before. However, I'm not really sure about what this error mean. I guess it
has to do with the fact that the mod defined has different dimensions (720
360 1 8), but I presume that was made in purpose...:-S

Any help will be really appreciated, I have this incompatibilities in
several scripts and I don't know if they come from the script itself or from
any problem with the inputs (which would be really annoying...)

Thank you in advance!

Cheers


--
View this message in context: http://r.789695.n4.nabble.com/number-of-items-to-replace-is-not-a-multiple-of-replacement-length-tp4634864.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list