Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use regular expression to change column names

the df column names are for instance:

split_the_tree1.zip.name, split_the_tree2.zip.region, split_the_tree3.zip.type

I would like to remove everything that comes before '.zip.' including the '.zip.'

To end up with column names: name, region, type

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried something like this:

gsub(".*\zip\/", "", colnames(df))

gsub('*.zip', '', colnames(df))

>Solution :

colnames(df) <- gsub('.*zip.', '', colnames(df))

Note that we have .* and not *.

Although the quick way to do this is to note that the names you want are simply stored as extensions, hence use tools::file_ext function

strings <- c("split_the_tree1.zip.name", "split_the_tree2.zip.region", 
             "split_the_tree3.zip.type")

tools::file_ext(strings)
[1] "name"   "region" "type"  
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading