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

#  names: The Names of an Object

##  Description

Functions to get or set the names of an object.

##  Usage

    
    
    names(x)
    names(x) <- value

##  Arguments

x

an  R  object.

value

a character vector of up to the same length as ` x ` , or ` NULL ` .

##  Value

For ` names ` , ` NULL ` or a character vector of the same length as ` x ` . (
` NULL ` is given if the object has no names, including for objects of types
which cannot have names.) For an environment, the length is the number of
objects in the environment but the order of the names is arbitrary.

For ` names<- ` , the updated object. (Note that the value of ` names(x) <-
value ` is that of the assignment, ` value ` , not the return value from the
left-hand side.)

##  Details

` names ` is a generic accessor function, and ` names<- ` is a generic
replacement function. The default methods get and set the ` "names" `
attribute of a vector (including a list) or pairlist.

For an ` [ environment ](/link/environment?package=base&version=3.6.2) ` ` env
` , ` names(env) ` gives the names of the corresponding list, i.e., `
names(as.list(env, all.names = TRUE)) ` which are also given by ` [ ls
](/link/ls?package=base&version=3.6.2) (env, all.names = TRUE, sorted = FALSE)
` . If the environment is used as a hash table, ` names(env) ` are its “keys”.

If ` value ` is shorter than ` x ` , it is extended by character ` NA ` s to
the length of ` x ` .

It is possible to update just part of the names attribute via the general
rules: see the examples. This works because the expression there is evaluated
as ` z <- "names<-"(z, "[<-"(names(z), 3, "c2")) ` .

The name ` "" ` is special: it is used to indicate that there is no name
associated with an element of a (atomic or generic) vector. Subscripting by `
"" ` will match nothing (not even elements which have no name).

A name can be character ` NA ` , but such a name will never be matched and is
likely to lead to confusion.

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

` [ slotNames ](/link/slotNames?package=base&version=3.6.2) ` , ` [ dimnames
](/link/dimnames?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%2Fnames&utm_source=r-docs&utm_medium=docs&utm_term=names&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    # print the names attribute of the islands data set
    names(islands)
    
    # remove the names attribute
    names(islands) <- NULL
    islands
    rm(islands) # remove the copy made
    
    z <- list(a = 1, b = "c", c = 1:3)
    names(z)
    # change just the name of the third element.
    names(z)[3] <- "c2"
    z
    
    z <- 1:3
    names(z)
    ## assign just one name
    names(z)[2] <- "b"
    z
    
    # }
    

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%2Fnames&utm_source=r-docs&utm_medium=docs&utm_term=names&utm_content=run_example_in_workspace)

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