| names | R Documentation |
Functions to get or set the names of an object.
names(x)names(x) <- value
x | anR object. |
value | a character vector of up to the same length as |
names is a generic accessor function, andnames<- is ageneric replacement function. The default methods get and setthe"names" attribute of a vector (including a list) orpairlist.
For anenvironmentenv,names(env) givesthe names of the corresponding list, i.e.,names(as.list(env, all.names = TRUE)) which are also given byls(env, all.names = TRUE, sorted = FALSE). If theenvironment is used as a hash table,names(env) are its“keys”.
Ifvalue is shorter thanx, it is extended by characterNAs to the length ofx.
It is possible to update just part of the names attribute via thegeneral rules: see the examples. This works because the expressionthere is evaluated asz <- "names<-"(z, "[<-"(names(z), 3, "c2")).
The name"" is special: it is used to indicate that there is noname associated with an element of a (atomic or generic) vector.Subscripting by"" will match nothing (not even elements whichhave no name).
A name can be characterNA, but such a name will never bematched and is likely to lead to confusion.
Both are primitive functions.
Fornames,NULL or a character vector of the same lengthasx. (NULL is given if the object has no names,including for objects of types which cannot have names.) For anenvironment, the length is the number of objects in the environmentbut the order of the names is arbitrary.
Fornames<-, the updated object. (Note that the value ofnames(x) <- value is that of the assignment,value, notthe return value from the left-hand side.)
For vectors, the names are one of the attributes withrestrictions on the possible values. For pairlists, the names are thetags and converted to and from a character vector.
For a one-dimensional array thenames attribute really isdimnames[[1]].
Formally classed aka “S4” objects typically haveslotNames() (and nonames()).
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
slotNames,dimnames.
# print the names attribute of the islands data setnames(islands)# remove the names attributenames(islands) <- NULLislandsrm(islands) # remove the copy madez <- list(a = 1, b = "c", c = 1:3)names(z)# change just the name of the third element.names(z)[3] <- "c2"zz <- 1:3names(z)## assign just one namenames(z)[2] <- "b"z
Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.