names() function in R Language is used to get or set the name of an Object. This function takes object i.e. vector, matrix or data frame as argument along with the value that is to be assigned as name to the object. The length of the value vector passed must be exactly equal to the length of the object to be named.
Syntax: names(x) <- value
Parameters:
x: Object i.e. vector, matrix, data frame, etc.
value: Names to be assigned to x
Example 1:
# R program to assign name to an object # Creating a vector x <- c(1, 2, 3, 4, 5) # Assigning names using names() function names(x) <- c("gfg1", "gfg2", "gfg3", "gfg4", "gfg5") # Printing name vector that is assigned names(x) # Printing updated vector print(x) |
Output:
[1] "gfg1" "gfg2" "gfg3" "gfg4" "gfg5" gfg1 gfg2 gfg3 gfg4 gfg5 1 2 3 4 5
Example 2:
# R program to get names of an Object # Importing Library library(datasets) # Importing dataset head(airquality) # Calling names() function to get names names(airquality) |
Output:
Ozone Solar.R Wind Temp Month Day 1 41 190 7.4 67 5 1 2 36 118 8.0 72 5 2 3 12 149 12.6 74 5 3 4 18 313 11.5 62 5 4 5 NA NA 14.3 56 5 5 6 28 NA 14.9 66 5 6 [1] "Ozone" "Solar.R" "Wind" "Temp" "Month" "Day"
Recommended Posts:
- Remove names or dimnames from an Object in R Programming - unname() Function
- Get or Set the Type of an Object in R Programming - mode() Function
- Get the number of columns of an Object in R Programming - ncol() Function
- Get the number of rows of an Object in R Programming - nrow() Function
- Get the Maximum element of an Object in R Programming - max() Function
- Get the Minimum element of an Object in R Programming - min() Function
- Search and Return an Object with the specified name in R Programming - get() Function
- Remove Duplicate Elements from an Object in R Programming - unique() Function
- Sort elements of an Object by its Index Values in R Programming - order() function
- Get Exclusive Elements between Two Objects in R Programming - setdiff() Function
- Get the Sign of Elements of a Numeric Vector in R Programming - sign() Function
- Applying a Function over an Object in R Programming - sapply() Function
- Get or Set Levels of a Factor in R Programming - levels() Function
- Get or Set Dimensions of a Matrix in R Programming - dim() Function
- Get or Set the Structure of a Vector in R Programming - structure() Function
- Get the First parts of a Data Set in R Programming - head() Function
- Get the Last parts of a Data Set in R Programming - tail() Function
- Get the List of Arguments of a Function in R Programming - args() Function
- Print the Value of an Object in R Programming - identity() Function
- Convert type of data object in R Programming - type.convert() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

