[R] Warning on assignment.

rkevinburton at charter.net rkevinburton at charter.net
Thu Jan 15 19:20:18 CET 2009


This was just an illustration. It is the warning message that I don't understand. The warning says "number of items to replace is not a multiple of replacement length". The way I look at it 10 is a multiple of 20.

Kevin

---- Sarah Goslee <sarah.goslee at gmail.com> wrote: 
> The lengths are different, particularly the length of subsetted x[i]
> 
> > x <- 1:20
> > i <- x %% 2 > 0
> > y <- rep(1,20)
> 
> > length(x)
> [1] 20
> > length(i)
> [1] 20
> > length(x[i])
> [1] 10
> > length(y)
> [1] 20
> 
> You happened to be lucky and got what you wanted, but a more reliable
> approach is:
> 
> > x[i] <- y[i]
> 
> Sarah
> 
> On Thu, Jan 15, 2009 at 1:08 PM,  <rkevinburton at charter.net> wrote:
> > I have a question on whether a warning message is valid or if I just don't understand the process. Let me illustrate via some R code:
> >
> > x <- 1:20
> > i <- x %% 2 > 0
> > y <- rep(1,20)
> >
> > x[i] <- y
> > Warning message:
> > In x[i] <- y :
> >  number of items to replace is not a multiple of replacement length
> >
> > But it still does what I would expect for the assignment:
> >
> >> x
> >  [1]  1  2  1  4  1  6  1  8  1 10  1 12  1 14  1 16  1 18  1 20
> >>
> >
> > What don't I understand?
> >
> > Thank you.
> >
> > Kevin
> >
> 
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org




More information about the R-help mailing list