Search code examples
rregexrenamesuffix

Rename suffix part of column name but keep the rest the same


For now I am redoing a merge because I poorly named the columns, however, I would like to know how to match on a suffix of a column name and rename that part of the column, keeping the rest the same.

For example, if I have a data.frame (could be a data.table too, doesn't matter - I could convert it):

d <- data.frame("ID" = c(1, 2, 3),
                "Attribute1.prev" = c("A", "B", "C"),
                "Attribute1.cur" = c("D", "E", "F"))

Now imagine that there are hundreds of columns similar to columns 2 & 3 from my sample DT. How would I go through and detect all columns ending in ".prev" change to ".1" and all columns ending in ".cur" change to ".2"?

So, the new column names would be: ID (unchanged), Attribute1.1, Attribute1.2 and so on for as many columns that match.


Solution

  • With base R we may do

    names(d) <- sub("\\.prev", ".1", sub("\\.cur", ".2", names(d)))
    d
    #   ID Attribute1.1 Attribute1.2
    # 1  1            A            D
    # 2  2            B            E
    # 3  3            C            F
    

    With the stringr package you could also use

    names(d) <- str_replace_all(names(d), c("\\.prev" = ".1", "\\.cur" = ".2"))
    

    If instead of Attribute1 and Attribute2 you may have some names with dots/spaces, you could also replace "\\.prev" and "\\.cur" patterns to "\\.prev$" and "\\.cur$" as to make sure that we match them at the end of the column names.