1
y <- rep(0, 10)
pryr::address(y)
y[3] <- 4
pryr::address(y)

output:

[1] "0x1fa4d8e0"
...
[1] "0x20cc70c0"

The address of y, the entire vector, is different after modifying just one element. This means that the whole vector was copied and moved. I was told that pre-allocating was the efficient way to fill a vector of unknown size in R. Actually, this is worst than growing a vector.

Am I missing something

EDITS:

I am using RStudio

0 Answers0