1

Does anybody know why the object 'r' presented in the image below is copied when its third entry is modified?

enter image description here

I only ask because recently I have been reading through Hadley Wickham's (fantastic) book Advanced R and he seemed to suggest in section 2.5 that objects will be modified in place if they only have one reference assigned to them. Is this not the case in the example I have constructed above?

Many thanks in advance.

Johnny
  • 285
  • 1
  • 7
  • 2
    I bet you are using Rstudio. The Rstudio environment window will also keep a reference. This probably would keep the same address if you were just using base R. – MrFlick Feb 18 '19 at 22:13
  • 2
    Possibly related: https://stackoverflow.com/questions/16906751/clarification-of-copying-array-semantics-in-r-on-assignment-to-array – MrFlick Feb 18 '19 at 22:17
  • 3
    [Operator “[<-” in RStudio and R](https://stackoverflow.com/questions/15559387/operator-in-rstudio-and-r?noredirect=1&lq=1); []() and 'Linked' therein. – Henrik Feb 18 '19 at 22:19
  • 1
    Hi both. Thanks a lot - you're both bang on the money. I was using R Studio and didn't realise that this was happening behind the scenes. – Johnny Feb 18 '19 at 22:24
  • 2
    See also [2.3 Copy-on-modify in Hadley's book](https://adv-r.hadley.nz/names-values.html#copy-on-modify): "When exploring copy-on-modify behaviour interactively, be aware that you’ll get different results inside of RStudio. That’s because the environment pane must make a reference to each object in order to display information about it." – Henrik Feb 18 '19 at 22:36
  • 2
    ...and from RStudio community: [In-place modification doesn't work in RStudio](https://community.rstudio.com/t/in-place-modification-doesnt-work-in-rstudio/9407) – Henrik Feb 18 '19 at 22:42

0 Answers0