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

Jim Lemon bitwrit at ozemail.com.au
Thu Sep 30 13:11:45 CEST 2004


Mag. Ferri Leberl wrote:
> What does this warning mean precisely?
> Is there any reason to care about it?
> Can I Avoid it by another way of programming?

As you have already gotten most of your questions answered, here is a 
possible answer to the last. You may want to care about it, but not 
necessarily. I have had to use the recycling capability of R without knowing 
whether the length of the result would be a multiple of that of the source 
vector. Say I want to label a vector of daily observations, but I do not know 
which day the observations start or how many there are:

label.weekdays<-function(ndays,start=1) {
 daynames<-rep(c("Sun","Mon","Tue","Wed","Thu","Fri","Sat"),ndays/7+1)
 return(daynames[start:(start+ndays-1)])
}

By creating a vector that is intentionally too long, then truncating it on 
one or both ends, I can recycle the source vector and avoid the warning.

Jim




More information about the R-help mailing list