# Create an unnamed vector my_data <- c(23, 5, 99) # Check the names (there are none!) names(my_data)
NULL
# Assign names using c() names(my_data) <- c("age", "height", "iq") # Print the data with names my_data
age height iq 23 5 99
Have you ever created a dataset in R and ended up with a bunch of unnamed elements? It can make your code clunky and hard to read. Fear not, fellow R wranglers! The names()
function is here to save the day.
Think of names()
as your data janitor, cleaning up and assigning names to the elements in your objects. It’s a chameleon, working with vectors, lists, data frames, and more!
names()
can be used in two ways:
Extracting Names: Want to see what names are already assigned? Simply use names(your_object)
. This will return a character vector showing the current names.
Assigning Names: Want to give your elements some meaningful titles? Use names(your_object) <- c("name1", "name2", ...)
. Here, c()
creates a character vector with your desired names, and the assignment operator (<-
) puts them in place.
# Create an unnamed vector my_data <- c(23, 5, 99) # Check the names (there are none!) names(my_data)
NULL
# Assign names using c() names(my_data) <- c("age", "height", "iq") # Print the data with names my_data
age height iq 23 5 99
In this example, we started with an unnamed vector. We then used names()
to see there were no existing names. Finally, we assigned clear names using c()
and the assignment operator.
# Create an unnamed list my_info <- list(score = 87, games = 10) # Peek at the names (default is numeric order) my_info
$score [1] 87 $games [1] 10
# Assign new names names(my_info) <- c("exam_score", "num_games") # Print the list with names my_info
$exam_score [1] 87 $num_games [1] 10
Here, we created a list with default numeric names. We used names()
to see these, then replaced them with more descriptive names.
# Sample data frame (mtcars comes with R) head(mtcars) # Peek at the data
mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
# Rename the "cyl" column names(mtcars)[[3]] <- "cylinders" # Access by position # Print the data frame with renamed column head(mtcars)
mpg cyl cylinders hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
This example shows how names()
can be used with data frames. We access the column position (index 3) and assign a new name using double square brackets ([[ ]]
).
Now it’s your turn! Grab some data and play with names()
. Here are some ideas:
By using names()
, you’ll make your code more readable and your data analysis smoother. Happy naming!