Home > Enterprise >  How do I remove _ Character(0) _ Character(0) from column header in R
How do I remove _ Character(0) _ Character(0) from column header in R

Time:06-28

I'm working on a df in R and the column headers are getting _ character(0) _ character(0) values too. I want to replace/remove them either with "NA" or "" so that I can trimws() to remove the spaces. How would I do that?

|S.no|A _ character(0) _ character(0)|B _character(0) _ character(0)| 
|:---|:-----------------------------|:----------------------------|
|1   | 20                           |55                           |
|2   | 30                           |56                           |

I tried colnames(df) <- gsub(" _ character(0)_ character(0)","",colnames(df)) so that I can trim the space later to just get the column header as

|S.no|A|B|
|:---|:|:|

But I'm still getting the same characters in the header.

CodePudding user response:

You will need to escape this character to make it work: (

df <- data.frame(`A _ character(0)_ character(0)` = 1, 
                 check.names = F)
df
  A _ character(0)_ character(0)
1                              1
colnames(df) <- gsub(" _ character\\(0)_ character\\(0)", "", colnames(df))

df
  A
1 1
  • Related