rr-colnames

R How to remove characters from long column names in a data frame


I have a large dataframe with long column names in it. I would like to shorten the columnnames by dropping characters before a colon sign (:), the sign is present in every column name in the dataframe columns. Looking for a way to perform this on a dataframe??


Solution

  • Perhaps (third try):

    names(df) <- sub("^(.+[:])", "", names(df))
    

    Read that regex as " starting at the beginning of the character string, consider all the characters up to and including the last instance of ":" as a character grouping and replace with a null-string. (It's the last ":" because regex matching is "greedy".)