Reputation: 5113
v <- c()
i <- 1
while (some_condition) {
v[i] <- some_value
i <- i + 1
}
So I am aware that each time v is modified a copy is made and v is moved. This is obviously very slow. This appears to be a trivia problem in other languages. What is the efficient way to do this in R?
Upvotes: 1
Views: 565
Reputation: 8846
As far as I'm aware there is no native method for populating a vector of unknown length without constantly rewriting it to memory. Maybe data.table
has some tricks?
Lacking any sophisticated solutions, something as simple as preallocating an oversized vector, as 12b345b6b78 suggest, can save you a fair bit of time.
unk <- 1e5
f1 <- function(unk) {
v <- c()
for (i in 1:unk) {
v[i] <- i
}
v
}
f2 <- function(unk) {
v <- vector(length=unk*2)
for (i in 1:unk) {
v[i] <- i
}
v[1:i]
}
f10 <- function(unk) {
v <- vector(length=unk*10)
for (i in 1:unk) {
v[i] <- i
}
v[1:i]
}
library(microbenchmark)
mb <- microbenchmark(f1(unk), f2(unk), f10(unk), times=50)
mb
# Unit: milliseconds
# expr min lq mean median uq max neval cld
# f1(unk) 27.177678 28.364024 32.65618 29.896673 36.18994 48.37088 50 c
# f2(unk) 8.075867 9.025156 10.87335 9.271589 10.07932 35.29222 50 a
# f10(unk) 11.132773 13.071857 20.46808 15.059086 21.53610 187.00786 50 b
Upvotes: 1