Unexpected class behavior <-` ()

I am having trouble understanding the difference in the following codes:

First part:

a <- 123
class(a) <- 'FOO'

b <- a
class(b) <- 'BAR'

class(a) # returns 'FOO'
class(b) # returns 'BAR'

The second part of:

a <- 123
`class<-`(a,'FOO')

b <- a 
`class<-`(b,'BAR')

class(a) # returns 'BAR' ! so class attribute has been replaced  also on "a"
class(b) # returns 'BAR'

As far as I know, I b <- ashould create a copy a, not immediately, but as soon as it is changed b.
But, looking at the second case, it seems that using the function `class<-(x,"")(which, as I expected, was just non-syntactic sugar class(x)<-""), the copy is not created, and the original object is modified.

Am I missing something (maybe in the documentation)?

Tested in version 3.2.5

+4
source share
1 answer

R, R. R-devel , , - . R-devel 70636.

, . - R ( ). class<- , a <- 'class<-'(a,'FOO') 'class<-'(a, 'FOO').

R (, 'class < -'), . a <- 'class<-'(a, 'FOO') , 'a' . C, R, , , , . , () ​​ class<- R.

, 'class<-' ( ) R-. , , 'class<-' . , AST . R ( AST) , , R-. R class(x)<-.

+4

Source: https://habr.com/ru/post/1659724/


All Articles