attr
Attribute of an Object
Description
Returns a specific attribute of an object. Attributes specify the
characteristics of any data object. This function can also be
used to create a new attribute.
Usage
attr(x, which, exact = FALSE)
attr(x, which) <- value
Arguments
x |
any object
|
which |
a character string specifying an attribute of x.
|
exact |
a logical value. If FALSE, which can be a partial attribute name.
The default is FALSE.
|
value |
an object or expression.
|
Details
attr can be used to set and get user-defined attributes.
To explicitly delete the which attribute of x, use
attr(x, which) <- NULL.
When you make an assignment, remember that the following attributes
accept only certain valid values. See their individual
topics for more information:
- names
- dim
- dimnames
- class
- comment
- row.names
- tsp
The semantics of attr are defined by saying that attr(x, which)
is equivalent to attributes(x)[[which]] for either extracting
or replacing attributes,
with the restriction that which is interpreted as mode character.
The value of attributes(x) is a list object (possibly of length 0)
associated implicitly with any object x.
See Subscript for the semantics of [[.
Value
attr(x, which) | returns the which attribute of
x, if it exists; otherwise, it returns NULL. |
attr(x, which) <- value | returns value. |
Side Effects
The assignment version of this function changes an attribute of
x.
Adding or removing the "dim" attribute typically affects
the class of x.
In some cases, attributes affect other attributes. For example,
assigning dim to a vector, a matrix, or an array removes
the names and dimnames attributes. See dim for
more information.
See Also
attributes,
length,
mode,
structure,
Subscript,
class,
dim,
dimnames,
levels,
names,
tsp
Examples
attr(Sdatasets::gas, "dimnames") # equivalent to dimnames(gas)
# Set and remove custom attributes
x <- c(a=7, b=11)
a <- list(one=13, two='foo')
attr(x, "myAttribute") <- a
identical(attr(x, "myAttribute"), a)
identical(attr(x, "my"), a) # partial matching
attr(x, "my", exact=TRUE) # exact matching, returns NULL
attr(x, "myAttribute") <- NULL