Thank you so much for showing the important step of saving the dataframe anew. I have been cleaning names but then would click on the dataframe in the Environment and couldn't figure out why the changes weren't showing up.
Yes, the way I would do it is using a regular expression like this: data %>% rename_all(. %>% str_replace_all("^CQ22[._]*", "")) which matches ^ start of string, CQ22, and any number of . or _ afterwards ........ or even .... data %>% rename_all(. %>% str_replace_all("^[^._]*[._]*", "")) if all the columns have the same format, which matches ^ start of the string, [^._]* any number of NON - '_ or .' characters, and [._]* any number of '_ or .' characters .... and replaces them with the empty string. (And then I would pass to janitor::clean_names(), e.g. data %>% rename_all(. %>% str_replace_all("^[^._]*[._]*", "")) %>% janitor::clean_names() but up to you!)
Thank you so much for showing the important step of saving the dataframe anew. I have been cleaning names but then would click on the dataframe in the Environment and couldn't figure out why the changes weren't showing up.
Simple and effective. It definitively saves me a lot of time when cleaning data. Thank you for sharing.
You're welcome!
thank you so so much i've been stuck on this for a while now
Great Tip, saves lot of time in cleaning column names. Thank you so much.
You bet!
That keyboard sound is underrated.
What's **your** favorite function in R that's underrated?
Very helpful. Thank you for this.
Hi, I just want to clean "CQ22." from following data. Is it possible?
CQ22.Allergy
CQ22._Chest_Pain
CQ22._Cough
CQ22._Hair_Loss
CQ22._Irritable_Mood
Yes, the way I would do it is using a regular expression like this:
data %>% rename_all(. %>% str_replace_all("^CQ22[._]*", ""))
which matches ^ start of string, CQ22, and any number of . or _ afterwards
........ or even ....
data %>% rename_all(. %>% str_replace_all("^[^._]*[._]*", ""))
if all the columns have the same format, which matches ^ start of the string, [^._]* any number of NON - '_ or .' characters, and [._]* any number of '_ or .' characters ....
and replaces them with the empty string.
(And then I would pass to janitor::clean_names(), e.g.
data %>% rename_all(. %>% str_replace_all("^[^._]*[._]*", "")) %>% janitor::clean_names()
but up to you!)
Simple but useful!