Use regular expression to change column names


the df column names are for instance:,,

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("", "", 

[1] "name"   "region" "type"  

Leave a Reply Cancel reply