Search code examples
rregexsplitnames

Split column names in R


I have a data frame like below.

df:

X1.Name    X1.ID  X1.Prac   X1.SCD

But, I need to split the column name by dot and display as,

output df:

Name   ID   Prac   SCD

Solution

  • Using sub:

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

    Demo

    The regex pattern I used will match everything from the start of the string up to, and including, the first dot. Then, it replaces that, and only that, with empty string.

    ^      from the start of the string
    [^.]+  match one or more characters which are NOT dots
    \\.    then match a literal dot
    

    We then replace this entire pattern with empty string "", i.e. we remove it from the original string.