I'm new to programming and using R
these days, and to concatenate new values to a result vector, I have been using
values = sample(letters, 1e4, replace=TRUE)
result_vector = NULL
for (i in 1:length(values)) result_vector = c(result_vector, values[i])
and recently I found myself pitiful when I measured the above,
result_vector = NULL
system.time( for (i in 1:length(values)) result_vector = c(result_vector, values[i]))
which gave me
user system elapsed
0.288 0.016 0.333
against an alternative,
result_vector = character(length(values))
system.time( for (i in 1:length(values)) result_vector[i] = values[i])
which gave me
user system elapsed
0.004 0.000 0.011
To learn from this enlightenment, I would like to ask what exactly happens when
result_vector = c(result_vector, new_value)
Is it reallocating a new space for result_vector
every iteration, which causes a lot of time?