4 C
London
Friday, December 20, 2024
HomeStatistics TutorialRHow to Rename a Single Column in R (With Examples)

How to Rename a Single Column in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

You can use one of the following methods to rename a single column in a data frame in R:

Method 1: Rename a Single Column Using Base R

#rename column by name
colnames(df)[colnames(df) == 'old_name'] new_name'

#rename column by position
#colnames(df)[2] new_name'

Method 2: Rename a Single Column Using dplyr

library(dplyr)

#rename column by name
df % rename_at('old_name', ~'new_name')

#rename column by position
df % rename_at(2, ~'new_name')

The following examples show how to use each method in practice with the following data frame in R:

#create data frame
df frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, 95),
                 assists=c(33, 28, 31, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      28       28
3    C     86      31       24
4    D     88      39       24
5    E     95      34       28

Example 1: Rename a Single Column Using Base R

The following code shows how to rename the points column to total_points by using column names:

#rename 'points' column to 'total_points'
colnames(df)[colnames(df) == 'points'] total_points'

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

The following code shows how to rename the points column to total_points by using column position:

#rename column in position 2 to 'total_points'
colnames(df)[2] total_points'

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

Notice that both methods produce the same result.

Example 2: Rename a Single Column Using dplyr

The following code shows how to rename the points column to total_points by name using the rename_at() function in dplyr:

library(dplyr)

#rename 'points' column to 'total_points' by name
df % rename_at('points', ~'total_points')

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

The following code shows how to rename the points column to total_points by column position using the rename_at() function in dplyr:

library(dplyr)

#rename column in position 2 to 'total_points'
df % rename_at(2, ~'total_points')

#view updated data frame
df

  team total_points assists rebounds
1    A           99      33       30
2    B           90      28       28
3    C           86      31       24
4    D           88      39       24
5    E           95      34       28

Notice that both methods produce the same result.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Select Specific Columns in R
How to Keep Certain Columns in R
How to Sort by Multiple Columns in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories