How can I rename a single column in R?

How can I rename a single column in R?

Renaming a single column in R is a simple and straightforward process that can be done using the `colnames()` function. First, the data frame containing the column that needs to be renamed is specified. Then, the `colnames()` function is used to assign a new name to the desired column. This will automatically update the name of the column in the data frame. It is important to note that the new name must be enclosed in quotes. Additionally, the `colnames()` function can also be used to rename multiple columns at once by passing a vector of new names. This allows for quick and efficient renaming of columns in R.

Rename a Single Column in R (With Examples)


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 <- df %>% rename_at('old_name', ~'new_name')

#rename column by position
df <- 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 <- data.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 :

library(dplyr)

#rename 'points' column to 'total_points' by name
df <- 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 :

library(dplyr)

#rename column in position 2 to 'total_points'
df <- 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.

Cite this article

stats writer (2024). How can I rename a single column in R?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-rename-a-single-column-in-r/

stats writer. "How can I rename a single column in R?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-rename-a-single-column-in-r/.

stats writer. "How can I rename a single column in R?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-rename-a-single-column-in-r/.

stats writer (2024) 'How can I rename a single column in R?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-rename-a-single-column-in-r/.

[1] stats writer, "How can I rename a single column in R?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I rename a single column in R?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top