attributes {base} | R Documentation |
Object Attribute Lists
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-handside of the assignment as the object's attributes (if appropriate).
Usage
attributes(x)attributes(x) <- valuemostattributes(x) <- value
Arguments
x | anyR object; for the replacement functions, not a |
value | an appropriate named |
Details
Unlikeattr
it is currently not an error to set attributeson aNULL
object: it will first be coerced to an emptylist
.
Note that some attributes (namelyclass
,comment
,dim
,dimnames
,names
,row.names
andtsp
) are treated specially and have restrictions onthe values which can be set. (Note that this is not true oflevels
which should be set for factors via thelevels
replacement function.)
Attributes are not stored internally as a list and should be thoughtof as a set and not a vector, i.e, theorder of the elements ofattributes()
does not matter. This is also reflected byidentical()
'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 ordergiven: this ensures that setting adim
attribute always precedesthedimnames
attribute.
Themostattributes
assignment takes special care for thedim
,names
anddimnames
attributes, and assigns them only when known to be valid whereas anattributes
assignment would give an error if any are not. Itis principally intended for arrays, and should be used with care onclassed objects. For example, it does not check thatrow.names
are assigned correctly for data frames.
The names of a pairlist are not stored as attributes, but are reportedas if they were (and can be set by the replacement form ofattributes
).
NULL
objects cannot have attributes and attempts toassign them will promote the object to an empty list.
Both assignment and replacement forms ofattributes
areprimitive functions.
References
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
See Also
Examples
x <- cbind(a = 1:3, pi = pi) # simple matrix with dimnamesattributes(x)## strip an object's attributes:attributes(x) <- NULLx # now just a vector of length 6mostattributes(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