Search code examples
rregexsubstrnames

Return a string between two characters '.'


I have column names similar to the following

names(df_woe)

# [1] "A_FLAG" "woe.ABCD.binned" "woe.EFGHIJ.binned"       
 ...

I would like to rename the columns by removing the "woe." and ".binned" sections, so that the following will be returned

names(df_woe)
# [1] "A_FLAG" "ABCD" "EFGHIJ"       
 ...

I have tried substr(names(df_woe), start, stop) but I am unsure how to set variable start/stop arguments.


Solution

  • nam <- c("A_FLAG", "woe.ABCD.binned", "woe.EFGH.binned")
    gsub("woe\\.|\\.binned", "", nam)
    [1] "A_FLAG" "ABCD"   "EFGH"  
    

    EDIT: a solution that deals with wierder cases such as woe..binned.binned

    gsub("^woe\\.|\\.binned$", "", nam)