How to Change the Case of Column Names in R [Examples]

In this article, we discuss how to change the case of column names (i.e., variable names) in R.

More specifically, we demonstrate how to convert column names to lowercase and uppercase (with R Base code and dplyr). Besides that, we also show how to capitalize the first letter of a column name.

The easiest way to change the case of a column name in R is by using the names() function and the tolower() (for lowercase) or toupper() (for uppercase) function. Alternatively, the rename_all() function from the dplyr package can also be used to (de)capitalize column names.

In this article, we discuss the different methods and provide examples that you can use directly in your R projects. Hence, this article is not about converting the values of a column name to lowercase or uppercase.

Are Column Names Case Sensitive in R?

Yes, column names are case-sensitive in R. That is to say, the R programming language treats columns in lowercase and uppercase letters as distinct. For example, it considers column1 and COLUMN1 as different columns.

As a result of being case-sensitive, combining R data frames with the same column names, but in different cases, can cause issues. For instance, when merging or joining two or more data frames. Therefore, it is crucial to align the case of the column (variable) names before carrying out operations.

Likewise, it’s also crucial that columns have the same case before you check if two data frames are equal. See our article where we discuss 3 ways to compare data frames.

How to Change Column Names to Lowercase in R?

In R, the easiest way to convert column names to lowercase is by using the functions names() and tolower().

First, the names() function reads the column names of a data frame and returns them in a character vector. Next, the tolower() function transforms all characters of this vector to lowercase. Lastly, the assign operator (i.e., <-) and the names() function assign the data frame its new, lowercase column names.

In summary:

  1. names() reads the original column names.
  2. tolower() converts the column names to lowercase.
  3. names() and <- assign the data frame the new, lowercase column names.

The R code below provides an example of these 3 steps.

my_df <- data.frame(NAME = c("Peter", "Ana", "John"),
                    AGE = c(32, 41, 28),
                    SALARY = c(35000, 50000, 31000))
my_df

names(my_df) <- tolower(names(my_df))
my_df
Change Case of Column Name in R to Lowercase

Instead of using the tolower() function, it is also possible to convert the case of column names to lowercase with the little-known casefold() function. An advantage of this function is versatility, it can transform letters to both lower- and uppercase. In this article, we discuss the casefold() function in more detail.

Convert Some Column Names to Lowercase

Above, we converted all column names to lowercase. However, it is also possible to transform the case of just some columns.

The easiest way to change the case of some specific column names in an R data frame is by using a vector and the square bracket notation. The vector and brackets are used to specify and select the column names, respectively. You can specify the column names either by their names or their positions.

For example, below we convert only the names of the second column and third column to lowercase.

my_df <- data.frame(NAME = c("Peter", "Ana", "John"),
                    AGE = c(32, 41, 28),
                    SALARY = c(35000, 50000, 31000))
my_df

names(my_df)[c(2,3)] <- tolower(names(my_df)[c(2,3)])
my_df

How to Change Column Names to Uppercase in R?

Like changing the column names of a data frame to lowercase, it is also possible to convert them into uppercase.

In R, the easiest way to change the column names to uppercase is by combining the power of the functions names() and toupper(). The names() function both reads the original column names and assigns the new, capitalized names to a data frame. In between these actions, the toupper() function converts the column names to uppercase.

The following R code demonstrates how to combine these functions and capitalize column names.

my_df <- data.frame(name = c("Peter", "Ana", "John"),
                    age = c(32, 41, 28),
                    salary = c(35000, 50000, 31000))
my_df

names(my_df) <- toupper(names(my_df))
my_df
Change Case of Column Name in R to Uppercase

Instead of using the toupper() function, the casefold() function converts characters to uppercase, too. Moreover, this function is more versatile than the toupper() function as it can also be used to transform column names to lowercase.

Transform Some Column Names to Uppercase

Rather than changing the case of all column names, R can also capitalize some variable names.

Besides the functions names() and toupper(), you need a vector and the square brackets to convert some column names to uppercase. The vector defines which columns to capitalize, whereas the square brackets filter those columns. Next, the names() function and the toupper() function convert the column names to uppercase.

The R code below contains an example. In this code, we capitalize only the first column and the third column name.

my_df <- data.frame(name = c("Peter", "Ana", "John"),
                    age = c(32, 41, 28),
                    salary = c(35000, 50000, 31000))
my_df

names(my_df)[c(1,3)] <- toupper(names(my_df)[c(1,3)])
my_df

The code above uses a vector to define the columns to be transformed based on their positions (i.e., 1 and 3). Alternatively, the vector might also contain the names of the vector. For example, c(“name”, “salary”).

How to Change the Case of Column Names with dplyr?

Many R programmers use the dplyr (or tidyverse) package for data preparation. The tools this package provides are extremely powerful and easy to understand. But, how do you convert column names to lower- or uppercase with dplyr?

You convert the case of all column names with dplyr by using the functions rename_all() and tolower() (for lowercase) or toupper() (for uppercase). Alternatively, the rename_at() function (de)capitalizes only explicitly specified columns based on either their name or position.

Below, we provide 4 examples of how to change the case of column names with dplyr.

We convert:

  1. All column names to lowercase.
  2. Some column names to lowercase.
  3. All column names to uppercase.
  4. Some column names to uppercase.

Remember, before you can use the rename_all() or rename_at() function, you need to (install and) load the dplyr package.


# Load dplyr package
library("dplyr")

# Convert all column names to lowercase
my_df <- data.frame(NAME = c("Peter", "Ana", "John"),
                    AGE = c(32, 41, 28),
                    SALARY = c(35000, 50000, 31000))

my_df %>% 
  rename_all(., .funs = tolower)

# Convert some column names to lowercase
my_df <- data.frame(NAME = c("Peter", "Ana", "John"),
                    AGE = c(32, 41, 28),
                    SALARY = c(35000, 50000, 31000))

my_df %>% 
  rename_at(c(2,3), .funs = tolower)

# Convert all column names to uppercase
my_df <- data.frame(name = c("Peter", "Ana", "John"),
                    age = c(32, 41, 28),
                    salary = c(35000, 50000, 31000))

my_df %>% 
  rename_all(., .funs = toupper)

# Convert some column names to uppercase
my_df <- data.frame(name = c("Peter", "Ana", "John"),
                    age = c(32, 41, 28),
                    salary = c(35000, 50000, 31000))

my_df %>% 
  rename_at(c("name", "salary"), .funs = toupper)

As the example shows, the rename_at() function allows both column positions or column names to define for which columns to change their case.

How to Capitalize the First Letter of a Column Name?

So far, we demonstrated how to convert column names to lower-/uppercase. However, how do you capitalize only the first letter of a column name?

In R, the easiest way to capitalize the first letter of a column name is by using the str_to_title() function and the rename_with() function. These functions are part of the stringr package and the dplyr package, respectively. However, it is recommended to use the tidyverse package instead (which both contains stringr and dplyr).

The rename_with() function changes column names. By providing the str_to_title() function as its argument, the rename_with() function converts the first letter of each column name to uppercase.

The str_to_title() function is part of the stringr package. This package provides many tools to process strings, e.g., the str_replace_all() function to remove blanks in column names.

The R code below contains an example to capitalize the first letter of each column name. Remember to (install and) load the tidyverse package first.

library(tidyverse)

my_df <- data.frame(name = c("Peter", "Ana", "John"),
                    age = c(32, 41, 28),
                    salary = c(35000, 50000, 31000))
my_df

my_df %>%
  rename_with(str_to_title)
Capitalize First Letter of Column Name in R