Reputation: 13
I have imported an XLSX files where each column title originally is formatted like "[idNo] nameNo". When I import the xlsx using read_excel, each column name is now:
"[id1]\nname1" "[id2]\nname2" "[id3]\nname3" ......... "[idn]\nnamen
How can I rename all the columns by looking right of the '\n' and retain the 'nameNo' only:
"name1" "name2" "name3" ....... "namen"
Thanks in advance
Upvotes: 1
Views: 178
Reputation: 522762
You could use sub
to strip off the unwanted content, for a base R option:
names(df) <- sub("^.*\n", "", names(df))
Using the dplyr
library:
df <- df %>% rename_with(~sub(".*\n", "", .))
Upvotes: 2
Reputation: 389325
You can remove everything until '\n'
names(df) <- sub('.*\n', '', names(df))
In dplyr
:
library(dplyr)
df <- df %>% rename_with(~sub('.*\n', '', .))
Upvotes: 0