In this data science tutorial, you will learn how to rename a column (or multiple columns) in R using base functions as well as dplyr. Renaming columns in R is a very easy task, especially using the rename() function. Now, renaming a column with dplyr and the rename() function is super simple. But, of course, it is not super hard to change the column names using base R as well.

  • Save
how to rename column in R

Now, there are some cases in which you need to get rid of strange column names such as “x1”, “x2”, “x3”. If we encounter data, such as this, cleaning up the names of the variables in our dataframes may be required and will definietly make work more readable. This is very important especially in those situations we are working together with others or share our data with with others. It is also very important that the column names have clear names if we plan to make the data open in a repository.

Outline

The outline of the post is a follows. First, you will learn about the requirements of this post. After you know what you need to follow this tutorial, you will get the answer to two questions. In the section, following the FAQs, we will load an example data set to work with. Here we will read an Excel file using the readxl package. When we have successfully imported data into R we can start by changing name on the oclumns. First, we will start by using a couple of techniques that can be done using base R. Second, we will work with dplyr. Specifically, in this section we will use the rename-family functions to change the names of some of the variables in the dataframe.That is, we will use the rename(), and rename_with().

Now, before going on to the next section it is worth mentioning that we can use dplyr to select columns as well as remove columns in R.

Requirements

To follow this post you need to have R installed as well as the packages readxl and dplyr. If you want to install the two packages you can use the install.packages() function. Here’s how to install readxl and dplyr: install.packages(c('dplyr', 'readxl').

It is worth pointing out, here, that both these packages are part of the Tidyverse. This means that you can install them, among with a bunch of other great packages, by typing install.packages('tidyverse').

How do I rename a column in R?

Rename Column in R
  • Save

You can rename a column in R in many ways. For example, if you want to rename the colunn called “A” to “B” you can use this code: names(dataframe)[names(dataframe)=="A"] <- "B". This way you changed the column name to "B".

How do I rename a column in dplyr in R?

To rename a column in R you can use the rename() function from dplyr. For example, if you want to rename the column "A" to "B", again, you can run the following code: rename(dataframe, B = A).

That was it, we are getting ready to practice how to change the column names in R. First, however, we need some data that we can practice on. In the next section, we are going to import data by reading a .xlsx file.

Example Data: Reading an Excel File

Here's how we can read a .xlsx file in R with the readxl package:

library(readxl) titanic_df <- read_excel('titanic.xlsx')
Code language: R (r)

In the code chunk above, we started by loading the library readxl and then we used the read_excel() function to read titanic.xlsx file. Here's the first 6 rows of this dataframe:

Dataframe to practice changing column names
  • Save

In the next section, we will start by using the base functionality to rename a column in R.

Rename Column with Base R Example 1: Using the Column Index

Here's how to rename a single column with base R:

names(titanic_df)[1] <- 'P_Class'
Code language: JavaScript (javascript)

In the code chunk above, we used the names()n function to assign a new name to the first column in the dataframe. Specifically, using the names()n function we get all the column names in the the dataframe and then we select the first columns using the brackets. Finally, we assigned the new column anme using the <- and the character 'P_Class' (the new name). Note, you can, of course, rename multiple columns in the dataframe using the same method as above. Just change what you put within the brackets. For example, if you want to rename columns 1 to 5 you can put "1:5" within the brackes and then a character vector with 5 column names.

R dataframe with renamed column
  • Save

In the next example, we are going to use the old column name, instead. to rename the column.

Rename Column with Base R Example 2: Using the Column Name

Here's how to change the column name by using the old name when selecting it:

names(titanic_df)[names(titanic_df) == 'P_Class'] <- "PCLASS'
Code language: JavaScript (javascript)

In the code chunk above, we did something quite similar as in the first method. However, here we selected the column we previously renamed by its name. This is what we do within the brackets. Notice how we, again, there used names and the == to select the column named "P_Class". Here's the output (new column name marked with red):

rename column in r
  • Save

In the next example, you will learn how to rename multiple columns using base R. In fact, we are going to rename all columns in the dataframe.

Rename all Columns with Base R Example 3

Renaming all columns can be done in a similar way as the last example. Here's how we change all the columns in the R dataframe:

names(titanic_df) <- c('PC', 'SURV', 'NAM', 'Gender', 'Age', 'SiblingsSPouses', 'ParentChildren', 'Tick', 'Cost', 'Cab', 'Embarked', 'Boat', 'Body', 'Home')
Code language: R (r)
all column names in dataframe changed
  • Save
all column names changed

Notice how we only used names() in the code above. Here it's worth knowing that if the character vector (right of the <-) should contain as many elements as there are column names. Or else, one or more columns will be named "NA". Morever, you need to know the order of the columns. In the next few examples, we are going to work with dplyr and the rename-family of functions.

You might also be interested in: How to use $ in R: 6 Examples – list & dataframe

How to Rename a Column in R with Dplyr

Renaming a column in dplyr is quite simple. Here's how to change a column name:

titanic_df <- titanic_df %>% rename(pc_class = PC)
Code language: R (r)

In the code chunk above, there are some new things that we work with. First, we start by importing dplyr. Second, we are changing the name in the dataframe using the rename() function. Notice how we use the %>% operator. This is very handy because the functions we use after this will be applied on the dataframe to the left of the operator. Third, we use the rename() function with one argument: the column we want to rename. For a blog post on another handy operator in R:

Remember, we renamed all of the columns in the previous example. In the code chunk above, we are actually changing the column back again. That is, to the left of = we have the new column name and to the right, the old name. As you will see in the next example, we can rename multiple columns in the dataframe by adding arguments.

  • Save

It may be worth mentioning that we can us dplyr to rename factor levels in R, and to add a column to a dataframe. In the next section, however, we are going to rename columns in R with dplyr.

How to Change Name of Columns in R with dplyr rename()

If we, on the other hand, want to change the name of multiple columns we can do as follows:

titanic_df <- titanic_df %>% rename(Survival = SURV, Name = NAM, Sibsp = SiblingsSPouses)
Code language: R (r)

It was quite simple to change the name multiple columns using dplyr's ename() function. As you can see, in the code chunk above, we just added each column that we wanted to change the name of. Again, the name to the right of the equal sign is the old column name. Here's the first 6 columns and rows of the dataframe with new column names marked with red:

  • Save
Three columns renamed

In the following sections, we will work with the rename_with() function. This is a great function which enables us to, as you will see, change the column names to upper or lower case.

How to Change all Column Names to Lowercase with dplyr rename_with()

Here's how we can use the rename_with() function (dplyr) to change all the column names to lowercase:

titanic_df <- titanic_df %>% rename_with(tolower)
Code language: R (r)

In the code chunk above, we used the rename_with() function and then the tolower() function. This function was applied on all the column names and the resulting dataframe look like this:

rename columns to lowercase dplyr
  • Save
column names changed to lowercase

In the next example, we are going to change the column names to uppercase using the rename_with() function together with the toupper() function.

How to Change all Column Names to Uppercase with dplyr's rename_with()

In this section, we wil just change the function that we use as the only argument in rename_with(). This will enable us to change all the oclumn names to uppercase:

titanic_df <- titanic_df %>% rename_with(toupper)
Code language: R (r)

Here's the first 6 rows where all the column names now is in uppercase:

  • Save

In the next section, we are going to continue working with the rename_with() function and see how we can use other functions to clean the column names from unwanted characters. For example, we can use the gsub() function to remove punctuation from column names.

How to Rename Column Names by Removing Punctuation

In some cases, our column names may contain characters that we don't really need. Here's how to use rename_with() from dplyr together with gsub() tro remove punctuation from all the column names in the R dataframe:

titanic_df <- titanic_df %>% rename_with(~ gsub('[[:punct:]]', '', .x))
Code language: JavaScript (javascript)

Notice how we added the tilde sign (~) before the gsub() function. Moreover, the first argument is the regular expression for punctuation and the second is what we want to remove it with. In our case, here, we just remove it from the column names. We could, however, add like an underscore ("_") if we want to replace the punctuation in the column names. Finally, if we wanted to replace specific characters we could add them as well, instead of the regular expression for punctuation.

Now that you have renamed the columns that needed better and clearer name you can continue with your data pre-processing. For example, you can add a column to the dataframe based on othher columns with dplyr, calculate descriptive statistics (also with dplyr), take the absolute value in your R dataframe, or remove duplicate rows or columns in the dataframe.

Conclusion: Rename Columns with Base R and dplyr

In this tutorial, you have learned how to use base R as well as dplyr. First, you learned how to use the base are functions to change the column name of a single columns based on their index and name. Second, you learned how to do the same with dplyr and the rename function. Here we also renamed multiple columns as well as removed punctuation from the column names. Hope you found the post useful. If you did, please share it on your social media accounts and link to it in your projects. Finally, if you have any corrections on the particular post or suggestion, both on this post or in general what should be covered on this blog, please let me know.

rename column in r
  • Save
Share via
Copy link
Powered by Social Snap