Quantcast

number of items to replace is not a multiple of replacement length

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

number of items to replace is not a multiple of replacement length

ascotilla
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
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: number of items to replace is not a multiple of replacement length

MacQueen, Don
Here is an example to use as a starting point for what that error message
means.

> x <- 1:6
>
> x[1:5] <- 1:2
Warning 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" <[hidden email]> 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,&quot;.nc&quot;,sep=&quot;&quot;)
>      
>      print(&quot;Opening:&quot;)
>      print(varname)
>      print(&quot;in:&quot;)
>      print(filename)
>      print(&quot;------------------------------------&quot;)
>      
>      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.
>
>______________________________________________
>[hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: number of items to replace is not a multiple of replacement length

ascotilla
Thanks a lot, I thought I was moving forward because I got a different error, but it seems that I didn't move forward at all..:-S...It was quite basic, right...?

That was very helpful, I'll try to sort out the dimensions problem now on my own...I want to fight a bit with it to see if I'm able to sort that out alone without embarrasing myself again..;-)

Cheers
Loading...