Search code examples
rcolumnsorting

Separate a column without separators


I have a dataset with a column that consists of a code and a number and I am trying to separate that column just to get the code.

df <- data.frame(code=rep(c('2d01', '2m04', '3C06', 'CrD05'), each=10),
                 pos=rep(c(3, 7,2,4), times=20))
df<-extract(df,code,into = c("code","number"),
            "(.{2})(.{2})")

I tried this but I have a code with a different number of characters (CrD05) so it doesn't work. Is there a way to separate the last 2 numbers and keep the rest?


Solution

  • Using gsub to extract the last two characters

    df$code2 = gsub("\\d{2}$", "", df$code)