adv-r
adv-r copied to clipboard
Subsetting without preserving classes
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