[R] problems with for: warnings and segfault
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Nov 11 17:05:44 CET 2005
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 message
At 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
More information about the R-help
mailing list