Search notes:

R function: attr

All objects can have named attributes whose values can be set and queried with attr().

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
Github repository about-r, path: /functions/attr/attr.R

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"
Github repository about-r, path: /functions/attr/remove-attribute.R

See also

Function attributes()
Index to (some) R functions

Index