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

  • 2
    Maybe I'm misunderstanding you, but aren't those two addresses the same? – joran Dec 05 '18 at 21:40
  • I fixed it. I copied and pasted the same address twice. But they are in fact different. You can try it on your own. – Bear Bile Farming is Torture Dec 05 '18 at 21:45
  • So, that's the thing, I did, and the addresses I get are the same. The rules R uses for when to copy objects are complicated and can depend on what other objects you have floating around and how they may/may not be connected. – joran Dec 05 '18 at 21:47
  • 1
    If you are using Rstudio, these are relevant: [Operator “[<-” in RStudio and R](https://stackoverflow.com/questions/15559387/operator-in-rstudio-and-r), [In-place modification doesn't work in RStudio](https://community.rstudio.com/t/in-place-modification-doesnt-work-in-rstudio/9407). Try in R GUI. – Henrik Dec 05 '18 at 21:47
  • I am using latest 3.5.1. And if I run all the lines at once then the address is the same, but if I run it line by line the address is different. Also, if use "<<-" instead then the address is the same even if I run it line by line – Bear Bile Farming is Torture Dec 05 '18 at 21:55

0 Answers0