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.
# Get the names of the object names(obj) # Set the names of the object names(obj) <- val
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
 "The names of vector: "  "Ozella Hoppe" "Markus Bins" "Sincere Lakin" "Kamryn Frami"  "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
# 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
 "The names of a data frame:  "names" "math" "physic" "chemistry"  "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
 "The names of a list: "  "Ozella Hoppe" "Markus Bins" "Sincere Lakin" "Kamryn Frami"  "The updated list:" $`Ozella Hoppe`  9 6 2 $`Markus Bins`  "LearnShareIT" $`Sincere Lakin`  20 $`Kamryn Frami`  "ABC"
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:
- Spread() Function In R: How To Use spread() In R
- Round() function in R: How To Use Round Function In R
- R Sample Function: How To Use The Sample() Function In R
Name of the university: HCMUS