2

Consider the following codes:

#rm(list=ls())
x <- 1:5
"[<-"(x, 2:3, value = 9:10)    
x

If I execute all the four lines at the same time, then x will be 1 9 10 4 5. If I execute the codes one by one, then in the end, x will be 1 2 3 4 5. What is the reason?

RHertel
  • 23,412
  • 5
  • 38
  • 64

1 Answers1

1

If you execute

x <- 1:5
"[<-"(x, 2:3, value = 9:10)    

you'll see the output resulting from the substitution:

1  9 10  4  5

However, the original value x will not be affected (you are not overwriting it)

Hence, if you execute x, you'll see its original value:

1 2 3 4 5

If I run all the lines at the same time (I guess you're using RStudio), I'll see this (I think this is why you're asking for)

execute> x <- 1:5
execute> "[<-"(x, 2:3, value = 9:10)    
[1]  1  9 10  4  5
execute> x
[1]  1  9 10  4  5

I guess this is some kind of weird RStudio behavior (maybe similar to this one), although, the value of x is not affected at all.

Note that it doesn't happen if you change the last line. E.g.

x <- 1:5
"[<-"(x, 2:3, value = 9:10)    
x <- 6:10

execute> x <- 1:5
execute> "[<-"(x, 2:3, value = 9:10)    
[1]  1  9 10  4  5
execute> x <- 6:10
execute> 

EDITED

Running with R version 3.3.1 and RStudio Version 0.99.467.

Running the three lines changes the value of x to 1 9 10 4 5. However, running the first line, and then both the second and the third, shows the right result (and the value of x is not affected)

execute> x <- 1:5
execute> "[<-"(x, 2:3, value = 9:10)    
[1]  1  9 10  4  5
execute> x
[1] 1 2 3 4 5
Community
  • 1
  • 1
Jaime Caffarel
  • 2,401
  • 4
  • 30
  • 42
  • Thanks for the help. I also think x should not change since there is no reassignment. R studio may somehow mess it up. – Zhaofeng Tang Aug 04 '16 at 14:42
  • Can't confirm. The value of `x` is changed to `1 9 10 4 5` in any case (R v3.3.1) , whether I run the code in RStudio or in a shell. It also makes no difference if the lines are executed sequentially or at once. – RHertel Aug 04 '16 at 14:42
  • Yeah, x changes to 1 9 10 4 5. Weird. – Jaime Caffarel Aug 04 '16 at 14:45
  • For me, it does make a difference. (R 3.3.1 and RStudio 0.99.467). If I execute the lines one by one, the final value of `x` is `1 2 3 4 5` . If I run all at once, `x` has a value of `1 9 10 4 5`. Could be a bug? – Jaime Caffarel Aug 04 '16 at 14:54
  • If I execute just the first line and then the two other lines, the result is correct (output of `1 9 10 4 5` and `x` is unchanged `1 2 3 4 5`). The "bad result" happens when I run the three lines. – Jaime Caffarel Aug 04 '16 at 14:59
  • Dunno. I guess for now, just add the details for each session/environment where you can repro what the OP is seeing, so your answer at least helps it to be pinned down. I assume you mean that you tested it in both Rgui and Rstudio? – Frank Aug 04 '16 at 14:59
  • @JaimeCr Maybe. I am using RStudio 0.99.902, if you only change the second element in x ("[<-"(x, 2, 9)), the x will always be 1 2 3 4 5. For R (3.3.0), x may be 1 2 3 4 5 or 1 9 10 4 5, when you shut down and reopen R, the answer may change – Zhaofeng Tang Aug 04 '16 at 15:02
  • @ZhaofengTang, yeah, I see the same result – Jaime Caffarel Aug 04 '16 at 15:06
  • 1
    @ZhaofengTang : With `"[<-"(x, 2, 9)`, "x" will never change because a new "double" "x" is created and assigned a "9" in its "2" position since you assign a "double" (9) to an "integer" ("x"). Using `9:10` you assign "integer"s to an "integer" so "x" should be overwritten. Why are you not using the `x[2:3] = 9:10` form? – alexis_laz Aug 04 '16 at 16:54