The Wayback Machine - https://web.archive.org/web/20240116235321/https://www.geeksforgeeks.org/get-or-set-names-of-elements-of-an-object-in-r-programming-names-function/
Open In App
Related Articles

Get or Set names of Elements of an Object in R Programming – names() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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"    
Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 05 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads