0

If I have a column with character variables that look like "1000_D_22", "1002M_26", and "1014_17_2/3/2019", how do I strip the characters so that I get "22", "26", and "17"?

Kevin Troy
  • 412
  • 4
  • 13
blaze
  • 57
  • 1
  • 3

1 Answers1

2

strsplit by non-number characters \D in regex, and then select [ the second value:

x <-  c("1000_D_22", "1002M_26", "1014_17_2/3/2019")
sapply(strsplit(x, "\\D+"), `[`, 2)
#[1] "22" "26" "17"
thelatemail
  • 91,185
  • 12
  • 128
  • 188