[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  attr: Object Attributes

##  Description

Get or set specific attributes of an object.

##  Usage

    
    
    attr(x, which, exact = FALSE)
    attr(x, which) <- value

##  Arguments

x

an object whose attributes are to be accessed.

which

a non-empty character string specifying which attribute is to be accessed.

exact

logical: should ` which ` be matched exactly?

value

an object, the new value of the attribute, or ` NULL ` to remove the
attribute.

##  Value

For the extractor, the value of the attribute matched, or ` NULL ` if no exact
match is found and no or more than one partial match is found.

##  Details

These functions provide access to a single attribute of an object. The
replacement form causes the named attribute to take the value specified (or
create a new attribute with the value given).

The extraction function first looks for an exact match to ` which ` amongst
the attributes of ` x ` , then (unless ` exact = TRUE ` ) a unique partial
match. (Setting ` [ options ](/link/options?package=base&version=3.6.2)
(warnPartialMatchAttr = TRUE) ` causes partial matches to give warnings.)

The replacement function only uses exact matches.

Note that some attributes (namely ` [ class
](/link/class?package=base&version=3.6.2) ` , ` [ comment
](/link/comment?package=base&version=3.6.2) ` , ` [ dim
](/link/dim?package=base&version=3.6.2) ` , ` [ dimnames
](/link/dimnames?package=base&version=3.6.2) ` , ` [ names
](/link/names?package=base&version=3.6.2) ` , ` [ row.names
](/link/row.names?package=base&version=3.6.2) ` and ` [ tsp
](/link/tsp?package=base&version=3.6.2) ` ) are treated specially and have
restrictions on the values which can be set. (Note that this is not true of `
[ levels ](/link/levels?package=base&version=3.6.2) ` which should be set for
factors via the ` levels ` replacement function.)

The extractor function allows (and does not match) empty and missing values of
` which ` : the replacement function does not.

` [ NULL ](/link/NULL?package=base&version=3.6.2) ` objects cannot have
attributes and attempting to assign one by ` attr ` gives an error.

Both are [ primitive ](/link/primitive?package=base&version=3.6.2) functions.

##  References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

##  See Also

` [ attributes ](/link/attributes?package=base&version=3.6.2) `

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fattr&utm_source=r-docs&utm_medium=docs&utm_term=attr&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    # create a 2 by 5 matrix
    x <- 1:10
    attr(x,"dim") <- c(2, 5)
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fattr&utm_source=r-docs&utm_medium=docs&utm_term=attr&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)