0

I have data set that contains several columns beginning with "sum of" (e.g., sum of Whites). I wonder how can I rename those columns by removing the "sum of" (e.g., sum of Whites--> Whites). It is of note that some of the columns in my data frame have a single word (e.g., Blacks) name and therefore the renaming noted is only needed for a few of the columns!

jpsmith
  • 11,023
  • 5
  • 15
  • 36
Nader Mehri
  • 514
  • 1
  • 5
  • 21

3 Answers3

1

In base R you can use gsub with names:

df <- data.frame(col1 = 1:5,
                 sum_of1a = 1:5,
                 sum_of2a = 1:5,
                 another_column = 1:5)


names(df) <- gsub("sum_of", "", names(df))

Or with dplyr:

df <- dplyr::rename_with(df, ~gsub("sum_of", "", .x))

Output (for both approaches):

#   col1 1a 2a another_column
# 1    1  1  1              1
# 2    2  2  2              2
# 3    3  3  3              3
# 4    4  4  4              4
# 5    5  5  5              5
jpsmith
  • 11,023
  • 5
  • 15
  • 36
  • 1
    It’s saying ‘NULL` because you’re asking for the class of a column that doesn’t exist anymore (in the second, `project$Sum of TotalTests` is now `project$TotalTests`) – jpsmith Jan 21 '23 at 05:28
1

You can use rename_with as -

library(dplyr)
library(stringr)

dat %>% rename_with(~str_remove(., 'sum of'), starts_with('sum of'))

#   Whites  Browns Blacks
#1       1       5      6
#2       2       4      7
#3       3       3      8
#4       4       2      9
#5       5       1     10

data

dat <- data.frame(`sum of Whites` = 1:5, `sum of Browns` = 5:1, 
                  `Blacks` = 6:10, check.names = FALSE)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Please check the below code where i used the iris dataframe and renamed the column Species to sum of Whites and then changing the name of the same to Whites

code

df <- iris %>% head %>% rename(`sum of Whites`=Species)

# get the names from the df and then change the name 
nam <- trimws(str_replace(names(df),'sum of',''))

# apply the names to the df
names(df) <- nam

output

  Sepal.Length Sepal.Width Petal.Length Petal.Width Whites
1          5.1         3.5          1.4         0.2 setosa
2          4.9         3.0          1.4         0.2 setosa
3          4.7         3.2          1.3         0.2 setosa
4          4.6         3.1          1.5         0.2 setosa
5          5.0         3.6          1.4         0.2 setosa
6          5.4         3.9          1.7         0.4 setosa

jkatam
  • 2,691
  • 1
  • 4
  • 12