Setting attributes
attr(obj, nam) <- val
sets the attribute named
nam
in the
object obj
to
val
.
attr(obj, nam)
returns the associated value of nam
in obj
.
obj <- c('Foo', 'Bar', 'Baz')
attr(obj, 'First attribute' ) <- "Value for 1st attribute"
attr(obj, 'Second attribute') <- 1:5
str(obj)
# atomic [1:3] Foo Bar Baz
# - attr(*, "First attribute")= chr "Value for 1st attribute"
# - attr(*, "Second attribute")= int [1:5] 1 2 3 4 5
cat("\n\n")
attributes(obj)
# $`First attribute`
# [1] "Value for 1st attribute"
#
# $`Second attribute`
# [1] 1 2 3 4 5
Removing attributes
An attribute can be removed from an object by assigning the
NULL
object to an attribute:
three <- 3;
attr(three, 'A') <- 'foo';
attr(three, 'B') <- 'bar';
attr(three, 'C') <- 'baz';
attr(three, 'B') <- NULL;
attributes(three);
#
# $A
# [1] "foo"
#
# $C
# [1] "baz"