[R] Adding values to the end of a vector?

Thomas Lumley tlumley at u.washington.edu
Tue Jan 4 18:16:50 CET 2005


On Tue, 4 Jan 2005, Daniel Almirall wrote:

>
> I am curious.  How are these suggestions different (better, worse?) from
>
> x <- NULL
> for (i in 1:5) x <- c(x, i)

One imporant difference is between solutions that preallocate storage and 
those that don't.
   x<-numeric(5)
   for(i in 1:5) x[i]<-i
allocates one vector of length 5 and then modifies it, but
   x<-NULL
   for(i in 1:5) x<-c(x,i)
allocates vectors of length 1, 2, 3, 4, 5 in turn.

You can't tell this from anything in the language definition, since 
conceptually x[i]<-i also copies: it does
    x <- "[<-"(x,i,i)
and for more complicated replacement functions it will really copy.  Even 
if the first version really copied there would be some potential for 
having more efficient memory allocation with all the objects being of size 
5 (at least, for very large values of 5).

If you don't know how long the vector needs to be then you can't 
preallocate, but a common programming strategy in other languages is to 
allocate powers of 2 (eg start out with x<-numeric(4) and if that isn't 
big enough do something like x<-c(x,numeric(4)) to double the size). I 
don't know if anyone has looked at whether this is ever useful in R.

 	-thomas

>
> Thanks,
> Danny
>
>
>
> On Tue, 4 Jan 2005, John Fox wrote:
>
>> Dear Dan,
>>
>> The following also works:
>>
>>> x <- numeric(0)
>>> for (i in 1:5) x[i] <- i
>>> x
>> [1] 1 2 3 4 5
>>
>> It's worth noting, however, that extending a vector in this manner can be
>> very inefficient for large vectors, since the vector is recopied each time.
>> If you can anticipate the number of elements (or place an upper bound on
>> it), then it's better to do something like
>>
>>> x <- numeric(5)
>>> for (i in 1:5) x[i] <- i
>>
>> I hope this helps,
>>  John
>>
>> --------------------------------
>> John Fox
>> Department of Sociology
>> McMaster University
>> Hamilton, Ontario
>> Canada L8S 4M4
>> 905-525-9140x23604
>> http://socserv.mcmaster.ca/jfox
>> --------------------------------
>>
>>> -----Original Message-----
>>> From: r-help-bounces at stat.math.ethz.ch
>>> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Liaw, Andy
>>> Sent: Tuesday, January 04, 2005 8:51 AM
>>> To: 'Dan Bolser'; R mailing list
>>> Subject: RE: [R] Adding values to the end of a vector?
>>>
>>> Is this what you're looking for?
>>>
>>>> x <- numeric(0)
>>>> for (i in 1:5) x <- append(x, i)
>>>> x
>>> [1] 1 2 3 4 5
>>>
>>> Andy
>>>
>>>
>>>> From: Dan Bolser
>>>>
>>>> I want to add values onto the end of a vector, for example...
>>>>
>>>> x <- vector
>>>>
>>>> for (i in 1:5){
>>>>   add_to_end_of_vector(i,x)
>>>> }
>>>>
>>>> I just cant find the answer to this question!
>>>>
>>>>
>>>> Sorry for such a basic question, I tried...
>>>>
>>>> x <- c()
>>>>
>>>> for (i in 1:5) x[length(x)] <- i
>>>>
>>>> but it didn't work.
>>>>
>>>> ______________________________________________
>>>> 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
>>>>
>>>>
>>>
>>> ______________________________________________
>>> 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
>>
>> ______________________________________________
>> 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
>>
>>
>>
>
> ______________________________________________
> 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
>

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle




More information about the R-help mailing list