Mag. Ferri Leberl
2004-Sep-29 13:03 UTC
[R] Warning: number of items to replace is not a multiple of replacement length
What does this warning mean precisely? Is there any reason to care about it? Can I Avoid it by another way of programming? Thank you in advance.
Paul Roebuck
2004-Sep-29 13:13 UTC
[R] Warning: number of items to replace is not a multiple ofreplacement length
On Wed, 29 Sep 2004, Mag. Ferri Leberl wrote:> What does this warning mean precisely?Usually means there isn't enough room to store your results.> Is there any reason to care about it?You probably should.> Can I Avoid it by another way of programming?Yes you can. Had you provided an example that caused the problem, you could have received more help! ---------------------------------------------------------- SIGSIG -- signature too long (core dumped)
Duncan Murdoch
2004-Sep-29 13:13 UTC
[R] Warning: number of items to replace is not a multiple of replacement length
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".
Barry Rowlingson
2004-Sep-29 13:41 UTC
[R] Warning: number of items to replace is not a multiple of replacement length
Mag. Ferri Leberl wrote:> What does this warning mean precisely?When R replaces parts of a vector with another vector, it repeats the replacing value until it is as long as the number of values it needs to replace. For example: > x = 1:10 > x[1:3]=1 - will do, effectively, x[1:3] = rep(1,3) Now, if you replace with a vector that isn't an integer multiple of the things being replaced, you get that error: > x[1:3]=c(1,2) Warning message: number of items to replace is not a multiple of replacement length Note that if it is an integer multiple, you don't get the error: > x[1:6]=c(1,2) but it repeats the (1,2) 3 times to fill the six places: > x [1] 1 2 1 2 1 2 7 8 9 10> Is there any reason to care about it?Yes, it usually means you've specified something wrong. What have you done? Baz
Yves Magliulo
2004-Sep-29 14:00 UTC
[R] Warning: number of items to replace is not a multiple of replacement length
hi, that means you're doing operation whith matrix or dataframe with row or column with different length. this can be a real reason to care about and sometimes it could be an error and not only a warning. you'd better adjust the length of your vector (array) to be the same in order to avoid this warning and be sure you're doing excatly what you want. here's a sample of what i meant.> toto=matrix(0,5,5) > toto[,1] [,2] [,3] [,4] [,5] [1,] 0 0 0 0 0 [2,] 0 0 0 0 0 [3,] 0 0 0 0 0 [4,] 0 0 0 0 0 [5,] 0 0 0 0 0> tata=c(1,2,3) > toto[,1]=tataError in "[<-"(*tmp*, , 1, value = tata) : number of items to replace is not a multiple of replacement length here's the correct syntax> toto[1:length(tata)]=tataLe mer 29/09/2004 ?? 15:03, Mag. Ferri Leberl a ??crit :> What does this warning mean precisely? > Is there any reason to care about it? > Can I Avoid it by another way of programming? > Thank you in advance. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- ------ Yves Magliulo <ym at climpact.com> R&D Engineer, CLIMPACT Tel. : +33 (0) 1 44 27 34 31 Fax. : +33 (0) 1 44 27 49 96 Universite Pierre et Marie Curie Boite 101 - Tour 45 - 5eme etage - Couloir 45/46 4 place Jussieu, 75252 Paris CEDEX 05, France
Jim Lemon
2004-Sep-30 11:11 UTC
[R] Warning: number of items to replace is not a multiple of replacement length
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