ascotilla
2012-Jun-29 11:25 UTC
[R] number of items to replace is not a multiple of replacement length
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.
MacQueen, Don
2012-Jun-29 16:08 UTC
[R] number of items to replace is not a multiple of replacement length
Here is an example to use as a starting point for what that error message means.> x <- 1:6 > > x[1:5] <- 1:2Warning message: In x[1:5] <- 1:2 : number of items to replace is not a multiple of replacement length The expression x[1:5] <- means that we are about to replace the first five elements of x with something. We then give it a vector of length two as that something. x[1:5] <- 1:2 It doesn't work, because 5 is not a multiple of 2. In contrast > x[1:4] <- 1:2succeeds. Whether or not it does what was intended is another question.> x <- 1:6 > x[1] 1 2 3 4 5 6> x[1:4] <- 1:2 > x[1] 1 2 1 2 5 6 Modifying the number of commas is unlikely to be the solution. It's also going to be very difficult to solve your problem remotely, because of not having the input data for one thing, also having no understanding of what the script is intended to do. Best guess is that your input data has different dimensions. Not how many dimensions, but the size of the dimensions. Couple of things you can do. Immediately after the error, type traceback() This might give a clue. Or, you can type debug(open_lpj_nc_gpp)before running the function. You can then step through the function and check the values of variables as you go. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 6/29/12 4:25 AM, "ascotilla" <mmartincal at gmail.com> wrote:>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. > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide >http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code.
Possibly Parallel Threads
- problem with loop to put data into array with missing data for some files
- problem with loop to put data into array with missing data forsome files
- comment lines sometimes removed from a function on exit from internal R editor
- Remove attribute from netcdf4 object
- missing values in 'ncdf', integer versus byte