Operator "[<-" in RStudio and R -
by accident i've encountered strange behaviour of "[<-"
operator. behaves differently depending on order of calls , whether i'm using rstudio or ordinary rgui. make myself clear example.
x <- 1:10 "[<-"(x, 1, 111) x[5] <- 123
as far know, first assigment shouldn't change x
(or maybe i'm wrong?), while second should do. , in fact result of above operations is
x [1] 1 2 3 4 123 6 7 8 9 10
however, when perform these operations in different order, results different , x
has changed! meaningly:
x <- 1:10 x[5] <- 123 "[<-"(x, 1, 111) x [1] 111 2 3 4 123 6 7 8 9 10
but happens when i'm using plain r! in rstudio behaviour same in both options. i've checked on 2 machines (one fedora 1 win7) , situation looks same. know 'functional' version ("[<-"(x..)
) never used i'm curious why happening. explain that?
==========================
edit: ok, comments reason x <- 1:10
has type 'integer' , after replacing x[5] <- 123
it's 'double'. still remains question why behaviour different in rstudio? restart r session , doesn't change anything.
rstudio's behavior
rstudio's object browser modifies objects examines in way forces copying upon modification. specifically, object browser employs @ least 1 r function call internally forces evaluation of object, in process resetting value of object's named field 1 2. r-internals manual:
when object altered, named field consulted. value of 2 means object must duplicated before being changed. [...] value of 1 used situations [...] in principle 2 copies of exist duration of computation [...] no longer, , primitive functions can optimized avoid copy in case.
to see object browser modifies named field ([nam()]
in next code block), compare results of running following lines. in first, both 'lines' run together, rstudio has no time 'touch' x
before structure queried. in second, each line pasted in separately, x
modified before examined.
## pasted in x <- 1:10; .internal(inspect(x)) # @46b47b8 13 intsxp g0c4 [nam(1)] (len=10, tl=0) 1,2,3,4,5,... ## pasted in delay between lines x <- 1:10 .internal(inspect(x)) # @42111b8 13 intsxp g0c4 [nam(2)] (len=10, tl=0) 1,2,3,4,5,...
once named field set 2, [<-(x, ...)
not modify original object. pasting following rstudio @ once modifies x
, while pasting in line-by-line not:
x <- 1:10 "[<-"(x, 1, 111)
one more consequence of rstudio's object browser makes operations slower otherwise be. again, compare same 2 commands first pasted in together, , 1 @ time:
## pasted in x <- 1:5e7 system.time(x[1] <- 9l) # user system elapsed # 0 0 0 ## pasted in 1 @ time x <- 1:5e7 system.time(x[1] <- 9l) # user system elapsed # 0.11 0.04 0.16
variable behavior of [<- in r
the behavior of [<-
w.r.t. modifying vector x
depends on storage types of x
, of element being assigned it. explains r
's behavior not rstudio's.
in r, when [<-
either appends vector x
, or performs subassignment requires x
's type modified, x
copied , value returned not overwrite pre-existing variable x
. (to need x <- "[<-(x, 2, 100)
.
so, neither of following modify x
x <- 1:2 ## note: typeof(x) --> "integer" ## subassignment requires x coerced "numeric" type "[<-"(x, 2, 100) ## note: typeof(100) --> "numeric" x # [1] 1 2 ## appending x "[<-"(x, 3, 100l) x # [1] 1 2
whenever possible, though, r allow [<-
function modify x
directly reference (i.e. without making copy). "possible" here includes cases in sub-assignment doesn't require x
's type modified.
so of following modify x
x <- c(0i, 0i, 0i, 0i) "[<-"(x, 1, true) "[<-"(x, 2, 20l) "[<-"(x, 3, 3.14) "[<-"(x, 4, 5+5i) x # [1] 1.00+0i 20.00+0i 3.14+0i 5.00+5i
Comments
Post a Comment