Taming the Nameless: Using the names() Function in R

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

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.

What is the names() function?

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!

How does it work?

names() can be used in two ways:

  1. 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.

  2. 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.

Let’s see it in action!

Example 1: Naming a Vector

# 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.

Example 2: Naming a List

# 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.

Example 3: Renaming Data Frame Columns

# 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 ([[ ]]).

Give it a Try!

Now it’s your turn! Grab some data and play with names(). Here are some ideas:

  • Create a vector of temperatures and name them for the days of the week.
  • Build a list of your favorite movies and assign names for genre and year.
  • Explore a built-in R dataset and rename some columns for clarity.

By using names(), you’ll make your code more readable and your data analysis smoother. Happy naming!

To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)