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) 
attr(x, which) <- value 

REQUIRED ARGUMENTS:

x
any object
which
character string specifying an attribute of x.

OPTIONAL ARGUMENTS:

value
a S-PLUS object or expression.

VALUE:

the which attribute of x, if it exists; otherwise, NULL. The assignment form returns value.

SIDE EFFECTS:

an attribute of x, and generally the class of x, will be changed as a result of the assignment version of this function. If the attribute did not previously exist, the class is usually changed to "structure", although some attributes (for example, "dim" and "levels" ) result in other classes (such as "array" or "matrix" , and "category" ).

In some cases attributes are mutually exclusive; for example, assigning names (a "names" attribute) to a matrix or array currently erases "dim" and "dimnames" attributes and changes a matrix or array to a vector or bdVector with class "named".

DETAILS:

Every data object receives two attributes ("length" and "mode") simply by being defined. "Length" specifies the number of values in the object and "mode" returns the type of object (for example, "bdFrame", "bdNumeric", "numeric", "list", "logical", "NULL"). "Length" and "mode" are implicit attributes.

Other attributes may define various data types in the object ("dim") or store labels for data in the object ("dimnames" or "names").

For the attributes like dim that are known internally to S-PLUS, the assignment is accepted only if it satisfies requirements for that attribute (for example, the length of x must equal the product of the dimensions). See the documentation for individual attributes. attr may also be used to set and get user-defined attributes. To explicitly delete the which attribute of x, use attr(x, which) <- NULL .

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 [[ .

SEE ALSO:

, , , , .

For attributes known to S-PLUS, see the documentation for , , , , and .

EXAMPLES:

attr(iris, "dimnames")     # equivalent to dimnames(iris) 
# a possible way to store documentation with x 
attr(x, "doc") <- doc.x