Hi, I have two problem with a for looping using R Version 2.1.1 (2005-06-20) on a Debian Linux Testing. The first problem: warnings messages Look:> xcoord <- 5 > ycoord <- 5 > indice <- 1 > for(i in c(1:5)) {indice <- indice+1;xcoord[indice] <- xcoord+i;ycoord[indice] <- ycoord } Warning messages: 1: number of items to replace is not a multiple of replacement length 2: number of items to replace is not a multiple of replacement length 3: number of items to replace is not a multiple of replacement length 4: number of items to replace is not a multiple of replacement length 5: number of items to replace is not a multiple of replacement length 6: number of items to replace is not a multiple of replacement length 7: number of items to replace is not a multiple of replacement length 8: number of items to replace is not a multiple of replacement length> xcoord[1] 5 6 7 8 9 10> ycoord[1] 5 5 5 5 5 5>The results are OK, but I dont understand the warning message The second problem: The segfault> xcoord <- 5 > ycoord <- 5 > indice <- 1 > for(i in c(1:100)) {indice <- indice+1;xcoord[indice] <- xcoord+i;ycoord[indice] <- ycoord } Segmentation fault This is a R bug or an error in my for function? Thanks Ronaldo -- Ningu??m ?? t??o mais inteligente que o outro para que o c??rebro conte mais que a perseveran??a --Alan Green -- |> // | \\ [***********************************] | ( ?? ?? ) [Ronaldo Reis J??nior ] |> V [UFV/DBA-Entomologia ] | / \ [36570-000 Vi??osa - MG ] |> /(.''`.)\ [Fone: 31-3899-4007 ] | /(: :' :)\ [chrysopa at insecta.ufv.br ] |>/ (`. `'` ) \[ICQ#: 5692561 | LinuxUser#: 205366 ] | ( `- ) [***********************************] |>> _/ \_Powered by GNU/Debian Woody/Sarge
"Ronaldo Reis-Jr." <chrysopa at gmail.com> writes:> Hi, > > I have two problem with a for looping using R Version 2.1.1 (2005-06-20) on a > Debian Linux Testing. > > The first problem: warnings messages > > Look: > > > xcoord <- 5 > > ycoord <- 5 > > indice <- 1 > > for(i in c(1:5)) {indice <- indice+1;xcoord[indice] <- xcoord+i; > ycoord[indice] <- ycoord } > Warning messages: > 1: number of items to replace is not a multiple of replacement length > 2: number of items to replace is not a multiple of replacement length > 3: number of items to replace is not a multiple of replacement length > 4: number of items to replace is not a multiple of replacement length > 5: number of items to replace is not a multiple of replacement length > 6: number of items to replace is not a multiple of replacement length > 7: number of items to replace is not a multiple of replacement length > 8: number of items to replace is not a multiple of replacement length > > > xcoord > [1] 5 6 7 8 9 10 > > ycoord > [1] 5 5 5 5 5 5 > > > > The results are OK, but I dont understand the warning messageLook at xcoord[indice] <- xcoord+i The left hand side is a single element, the right hand side is a vector so I don't think it means what I think you think it means.> The second problem: The segfault > > > xcoord <- 5 > > ycoord <- 5 > > indice <- 1 > > for(i in c(1:100)) {indice <- indice+1;xcoord[indice] <- xcoord+i; > ycoord[indice] <- ycoord } > Segmentation fault > > This is a R bug or an error in my for function?R shouldn't segfault no matter how silly the code.... [I see this on 2.2.0/Linux too, on the 2nd try of the for loop] -- O__ ---- Peter Dalgaard ??ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Fri, 11 Nov 2005, Ronaldo Reis-Jr. wrote:> Hi, > > I have two problem with a for looping using R Version 2.1.1 (2005-06-20) on a > Debian Linux Testing. > > The first problem: warnings messages > > Look: > >> xcoord <- 5 >> ycoord <- 5 >> indice <- 1 >> for(i in c(1:5)) {indice <- indice+1;xcoord[indice] <- xcoord+i; > ycoord[indice] <- ycoord } > Warning messages: > 1: number of items to replace is not a multiple of replacement length > 2: number of items to replace is not a multiple of replacement length > 3: number of items to replace is not a multiple of replacement length > 4: number of items to replace is not a multiple of replacement length > 5: number of items to replace is not a multiple of replacement length > 6: number of items to replace is not a multiple of replacement length > 7: number of items to replace is not a multiple of replacement length > 8: number of items to replace is not a multiple of replacement length > >> xcoord > [1] 5 6 7 8 9 10 >> ycoord > [1] 5 5 5 5 5 5 >> > > The results are OK, but I dont understand the warning messageAt step one you have xcoord[2] <- 5+1, so xcoord is c(5,6) At step two you have xcoord[3] <- xcoord+2, so you are trying to replace one value with two, and the same for ycoord. ... It is much better practice to create a vector with the size you are going to need it.> The second problem: The segfault > >> xcoord <- 5 >> ycoord <- 5 >> indice <- 1 >> for(i in c(1:100)) {indice <- indice+1;xcoord[indice] <- xcoord+i; > ycoord[indice] <- ycoord } > Segmentation fault > > This is a R bug or an error in my for function?Both since R should not segfault, but mainly the latter. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595