names in R: How To Use The names() Function in R?

Names Function in R

Today, we will learn how to use the names Function in R together. If you are interested in this topic, please read this article to the end, as it will help you get a lot of helpful information.

What is names() function in R?

We can use the names() function to get or set the name of an object in R. This function takes an object, e.g., matrix, data frame, vector, as an argument with the value assigned as a name to the object.

Syntax:

# Get the names of the object
names(obj)

# Set the names of the object
names(obj) <- val

Parameters:

  • obj: The object in R, e.g., matrix, data frame, vector, …
  • val: The value is assigned to the obj.

How to use the names function in R?

Example 1: Using names function with vector

Here, we will build a program using the names function with vectors. See the code below to understand better.

# Create a vector
myVector <- c(15, 30, 65, 48)

# Set the names for vector
names(myVector) <- c('Ozella Hoppe', 'Markus Bins', 'Sincere Lakin', 'Kamryn Frami')

# Print names vector, which is assigned
print("The names of vector: ")
names(myVector)

# Print updated vector
print('The updated vector:')
myVector

Output

[1] "The names of vector: "
[1] "Ozella Hoppe"  "Markus Bins"   "Sincere Lakin" "Kamryn Frami"
[1] "The updated vector:"
Ozella Hoppe   Markus Bins Sincere Lakin  Kamryn Frami
     15            30            65            48

Example 2: Using names function with data frame

We will use the names() function to get and set the names for the columns of a data frame:

# Create a data frame
dataFrame <- data.frame(A = c('Ozella Hoppe', 'Markus Bins', 
                              'Sincere Lakin', 'Kamryn Frami'),
                        B = c(9, 5, 6, 8),
                        C = c(6, 8, 9, 10),
                        D= c(8, 6, 9, 4))

# Set the names for a data frame
names(dataFrame) <- c('names', 'math', 'physic', 'chemistry')

# Print names data frame, which is assigned
print("The names of data frame: ")
names(dataFrame)

# Print updated data frame 
print("The updated data frame:")
dataFrame

Output

[1] "The names of a data frame:
[1] "names"     "math"      "physic"    "chemistry"
[1] "The updated data frame:"
          names         math  physic  chemistry
1  Ozella Hoppe          9      6          8
2   Markus Bins          5      8          6
3 Sincere Lakin          6      9          9
4  Kamryn Frami          8      10         4

Example 3: Using the names function with a list

The program example for getting the name of a list.

# Create a list
myList <- list(c(9, 6, 2), 'LearnShareIT', 20, 'ABC')

# Set the names for a list
names(myList) <- c("Ozella Hoppe", "Markus Bins", "Sincere Lakin", "Kamryn Frami")

# Print names list, which is assigned
print("The names of a list: ")
names(myList)

# Print updated list
print('The updated list:')
myList

Output

[1] "The names of a list: "
[1] "Ozella Hoppe"  "Markus Bins"   "Sincere Lakin" "Kamryn Frami"
[1] "The updated list:"
$`Ozella Hoppe`
[1] 9 6 2

$`Markus Bins`
[1] "LearnShareIT"

$`Sincere Lakin`
[1] 20

$`Kamryn Frami`
[1] "ABC"

Summary

As we said, this article has brought you a lot of useful knowledge about names in R, right? If you have any questions, please leave a comment below. And finally, have a nice day!

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *