R Basic

R names() Function

The names() function in R gets or sets the names of objects such as vectors, lists, or data frames. If you are using it to fetch the name, it will return a character vector containing the names assigned to its elements or columns.

Syntax

names(obj) # To get names

names(obj) <- value # To set names

Parameters

Name Value
obj It is an input object whose name you want to set or get.
value It is a character vector of the same length as the input object.

Vectors

Let’s declare a vector and try to get its name.

vec_obj <- c(11, 21, 19, 18)

names(vec_obj) # Output: NULL

You can see that since we did not assign any name to it, by default, it is NULL.

Let’s set the name of the vector and get it:

vec_obj <- c(11, 21, 19, 18)

names(vec_obj) # Output: NULL

names(vec_obj) <- c("a", "b", "c", "d")

names(vec_obj) # Output: [1] "a" "b" "c" "d"

Here, we set the name using the name(obj) <- value syntax where obj is our vec_obj and value is our new character vector.

You can also analyze that the length of vec_obj is the same as the character_vector (name) we assigned to the object.

Now, you can access the individual elements of a vector by their names.

vec_obj <- c(11, 21, 19, 18)

names(vec_obj) <- c("a", "b", "c", "d")

vec_obj["b"] # Output: 21

Lists

Let’s define a list with some elements and use the names() function to get the names.

# Create a list
list_obj <- list(name = "Krunal", age = 31, city = "Rajkot")

# Get names
names(list_obj) # Output: "name" "age" "city"

# Accessing elements by name
person$age # Output: 27

Here, the names() function retrieves the keys(names) of the elements of the list. The names are: “name”, “age”, and “city”.

Data Frames

When you are working with data frames, and you will use the names() function, it will return the names of the columns.

# Creating a data frame
df <- data.frame(
  Name = c("Krunal", "Ankit", "Dhaval"),
  Age = c(25, 30, 22),
  City = c("London", "Paris", "Perth")
)

# Getting column names
names(df) # Output: "Name" "Age" "City"


# Accessing columns by name
df$Age # Output: 25 30 22

Removing names

If you want to remove names from an object, assign that object’s name to NULL.

names(obj) <- NULL

For matrices, there are already two functions available: rownames() and colnames().

That’s it!

Recent Posts

R length(): Vector, List, Matrix, Array, Data Frame, String

Before executing an operation on an object, it is advisable to check its length, as…

15 hours ago

How to Round Numbers in R

Rounding is a process of approximating a number to a shorter, simpler, and more interpretable…

2 days ago

Adding Single or Multiple Columns to Data Frame in R

Whether you want to add new data to your existing datasets or create new variables…

4 days ago

sqrt() Function: Calculate Square Root in R

The square root of a number is a value that is multiplied by itself, giving…

5 days ago

How to Remove Duplicate Rows from DataFrame in R

Duplicate rows refer to all the values across all columns that are the same in…

6 days ago

How to Remove NA From Vector in R

A vector is a data structure that holds the same type of data. When working…

1 week ago