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

Duncan Murdoch murdoch at stats.uwo.ca
Wed Sep 29 15:13:58 CEST 2004

On Wed, 29 Sep 2004 15:03:24 +0200, "Mag. Ferri Leberl"
<ferri.leberl at gmx.at> wrote :

>What does this warning mean precisely?

You get this when you do something like this:

x <- 1:11
y <- 1:2

x[1:11] <- y

In the last line, R makes 11 assignments, reusing the values in y[1]
six times and y[2] five times.

>Is there any reason to care about it?

You rarely want recycling to work that way.  It's usually a sign that
you thought y contained a single value, and you wanted it repeated
throughout x.

>Can I Avoid it by another way of programming?

Usually correcting your code removes the message.  If you really
wanted this without the warning, you could do it in two lines:

x[1:10] <- y
x[11] <- y[1]

Duncan Murdoch

P.S. Your reply address ferri.leberl at gmx.at doesn't work; I get a
"mailbox disabled message".

More information about the R-help mailing list