0

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?

  • 1
    We could use `"(.*)(\\d{2}$)"` as a pattern – Aurèle Nov 16 '21 at 13:38
  • 1
    [This is a similar question with nice answers.](https://stackoverflow.com/questions/15897236/extract-the-first-or-last-n-characters-of-a-string) – MSD20 Nov 16 '21 at 14:01

1 Answers1

0

Using gsub to extract the last two characters

df$code2 = gsub("\\d{2}$", "", df$code)
Bastian Andres
  • 198
  • 1
  • 8