Reputation: 443
I have an R dataframe with column names as following,
MMR42_L_2_S52_L001_R1_001
MMR42_LN_2_S51_L001_R1_001
MMR43_N_1_S53_L001_R1_001
MMR48_N_1_S54_L001_R1_001
MMR612_S55_L001_R1_001
MMR658_S56_L001_R1_001
I have to remove the _S* from each column name
Desired Column names:
MMR42_L_2
MMR42_LN_2
MMR43_N_1
MMR48_N_1
MMR612
MMR658
My Idea
library(dplyr)
df1 %>%
rename_all(.funs = funs(sub("\\_S*", "", names(df1)))) %>%
I could not get the desired result with the above
Upvotes: 1
Views: 622
Reputation: 887158
Within the rename_at/_all
, the .
is the column name. We don't need names(.)
library(dplyr)
library(stringr)
df1 %>%
rename_all(~ str_remove(., "\\_S.*"))
Or using the OP's code
df1 %>%
rename_all(.funs = funs(sub("\\_S.*", "", .)))
Upvotes: 2