R: Object Attribute Lists (original) (raw)
attributes {base} | R Documentation |
---|
Description
These functions access an object's attributes. The first form below returns the object's attribute list. The replacement forms uses the list on the right-hand side of the assignment as the object's attributes (if appropriate).
Usage
attributes(x)
attributes(x) <- value
mostattributes(x) <- value
Arguments
x | any R object; for the replacement functions, not asymbol (aka ‘name’) nor a primitive function. |
---|---|
value | an appropriate named list of attributes, orNULL. |
Details
Unlike [attr](../../base/help/attr.html)
it is currently not an error to set attributes on a NULL
object: it will first be coerced to an empty[list](../../base/help/list.html)
.
Note that some attributes (namely [class](../../base/help/class.html)
,[comment](../../base/help/comment.html)
, [dim](../../base/help/dim.html)
, [dimnames](../../base/help/dimnames.html)
,[names](../../base/help/names.html)
, [row.names](../../base/help/row.names.html)
and[tsp](../../stats/html/tsp.html)
) are treated specially and have restrictions on the values which can be set. (Note that this is not true of[levels](../../base/help/levels.html)
which should be set for factors via thelevels
replacement function.)
Attributes are not stored internally as a list and should be thought of as a set and not a vector, i.e, the order of the elements ofattributes()
does not matter. This is also reflected by[identical](../../base/help/identical.html)()
's behaviour with the default argumentattrib.as.set = TRUE
. Attributes must have unique names (andNA
is taken as "NA"
, not a missing value).
Assigning attributes first removes all attributes, then sets anydim
attribute and then the remaining attributes in the order given: this ensures that setting a dim
attribute always precedes the dimnames
attribute.
The mostattributes
assignment takes special care for the[dim](../../base/help/dim.html)
, [names](../../base/help/names.html)
and [dimnames](../../base/help/dimnames.html)
attributes, and assigns them only when known to be valid whereas anattributes
assignment would give an error if any are not. It is principally intended for arrays, and should be used with care on classed objects. For example, it does not check that[row.names](../../base/help/row.names.html)
are assigned correctly for data frames.
The names of a pairlist are not stored as attributes, but are reported as if they were (and can be set by the replacement form ofattributes
).
[NULL](../../base/help/NULL.html)
objects cannot have attributes and attempts to assign them will promote the object to an empty list.
Both assignment and replacement forms of attributes
areprimitive functions.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language. Wadsworth & Brooks/Cole.
See Also
[attr](../../base/help/attr.html)
, [structure](../../base/help/structure.html)
.
Examples
x <- cbind(a = 1:3, pi = pi) # simple matrix with dimnames
attributes(x)
## strip an object's attributes:
attributes(x) <- NULL
x # now just a vector of length 6
mostattributes(x) <- list(mycomment = "really special", dim = 3:2,
dimnames = list(LETTERS[1:3], letters[1:5]), names = paste(1:6))
x # dim(), but not {dim}names
[Package _base_ version 4.6.0 Index]