How to rename a column with unexpected signs using dplyr?

Hi
I am a biginer in R. I have uploaded a csv file through R import and I have realised it contains some strange name, like "Company \r\n(Maker-if known)" After trying to rename using the rename (data name, new name = old name) code, the output comes with a warning saying "unexpected '\' how could I rename please?

Thank you

Can you copy and paste the exact code you used? Paste it here with three backticks before and three after.

flavor1<-rename(flavor, company=flavor$Company (Maker-if known))

Try

flavor1<-rename(flavor, company=`Company (Maker-if known)`)
1 Like

You can also use function clean_names() from the package janitor:

library(janitor)
flavor <- read.csv("your_data_file.csv") |>
  janitor::clean_names()

clean_names() will convert all the dodgy column names into proper R names, automatically.

1 Like

Thank you so much. The code of flavor2<- janitor::clean_names(flavor1) has cleaned all unexpected signs. then the code flavor3<- flavor %>% rename(desired name= existed name) has worked again without warning.

Thank you so muc.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.