adv-r icon indicating copy to clipboard operation
adv-r copied to clipboard

Subsetting without preserving classes

Open KZARCA opened this issue 5 years ago • 0 comments

Hi, Reading the book, I thought that subsetting with [ was preserving attributes as well. It is not the case for vectors and lists

a <- structure(as.list(letters[1:5]), class=c("list", "otherclass"))
class(a)
#> [1] "list"       "otherclass"
class(a[1])
#> [1] "list"
a <- structure(c(a=1, b=2, c=3), at=c("attr1", "attr2"))
attributes(a)
#> $names
#> [1] "a" "b" "c"
#> 
#> $at
#> [1] "attr1" "attr2"
attributes(a[1])
#> $names
#> [1] "a"

Best

KZARCA avatar Mar 03 '20 14:03 KZARCA