I am confused about the preallocation in R. We all know that preallocation will be much faster and it can solve the problems of over-memory. However, if we want to obtain a list with an unknown length, should we assign a much longer length? Or we could assign a "suitable" length and if the final length of the list is over than the assigned length, we can then append the extended elements to the list. For example, we assume the length of the final list is 20000.
The first method will be:
# the length of A should be unknown, I just set it as 20000 here for convenience
A <- rep(1:10, each=2000)
x <- vector(mode = "list", length = 50000)
for (i in 1:length(x)) {x[i] <- A[i]}
x <- x[!is.na(x)]
#or
x <- na.omit(x)
The second method will be:
A <- rep(1:10, each=2000)
x <- vector(mode = "list", length = 10000)
for (i in 1:length(A)) {
if (i<=length(x)) {
x[i] <- A[i]
}
else {
x <- c(x, A[i])
}
}
Which one will be better? Or please let me know if you have better ideas? Thanks.