Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing suffix from column names using rename_all?

Tags:

r

tidyverse

I have a data frame with a number of columns in a form var1.mean, var2.mean. I would like to strip the suffix ".mean" from all columns that contain it. I tried using rename_all in conjunction with regex in a pipe but could not come up with a correct syntax. Any suggestions?

like image 779
linda Avatar asked Aug 30 '17 12:08

linda


People also ask

How do I remove suffix from column names in R?

To remove all columns with a common suffix from an R data frame, you need to use the grep() function. This function identifies and returns a vector with all columns that share the suffix. You can use this vector as an argument of the select() function to remove the columns from the data frame.

How do I remove a string from a column name in R?

To remove a character in an R data frame column, we can use gsub function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.

How do I rename a column 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) .

How do I rename column names?

To change a column name, enter the following statement in your MySQL shell: ALTER TABLE table_name RENAME COLUMN old_column_name TO new_column_name; Replace table_name , old_column_name , and new_column_name with your table and column names.


3 Answers

If you want to use the dplyr package, I'd recommend using the rename_at function.

Dframe <- data.frame(var1.mean = rnorm(10),
                     var2.mean = rnorm(10),
                     var1.sd = runif(10))

library(dplyr)

Dframe %>% 
  rename_at(.vars = vars(ends_with(".mean")),
            .funs = funs(sub("[.]mean$", "", .)))
like image 197
Benjamin Avatar answered Oct 17 '22 13:10

Benjamin


Using new dplyr:

df %>% rename_with(~str_remove(., '.mean'))
like image 19
Reza Avatar answered Oct 17 '22 12:10

Reza


We can use rename_all

df1 %>%
   rename_all(.funs = funs(sub("\\..*", "", names(df1)))) %>%
   head(2)
#        var1        var2       var3       var1       var2       var3
#1 -0.5458808 -0.09411013  0.5266526 -1.3546636 0.08314367  0.5916817
#2  0.5365853 -0.08554095 -1.0736261 -0.9608088 2.78494703 -0.2883407

NOTE: If the column names are duplicated, it needs to be made unique with make.unique

data

set.seed(24)
df1 <- as.data.frame(matrix(rnorm(25*6), 25, 6, dimnames = list(NULL,
             paste0(paste0("var", 1:3), rep(c(".mean", ".sd"), each = 3)))))
like image 10
akrun Avatar answered Oct 17 '22 13:10

akrun