Use regular expression to change column names

Advertisements

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

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"  

Leave a Reply Cancel reply