1

I have some issues while working with text stings in R.

I have some string vector which consists of, combined, characters and numbers:

> a <- c("China ltd 123100","Morocco125312 Ytd")
> a

[1] "China ltd 123100"  "Morocco125312 Ytd"

The thing is that every element of a contains a sequence of 6 numbers and I need to extract first 3 numbers of every sequence.

Output should be:

[1] "123"  "125"

I need to do this with basic R functions since I am not able to install packages. Can you please help me, have no ideas how to do this.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83

2 Answers2

2

Maybe you can try gsub like below

> gsub("\\D+(\\d{3}).*","\\1",a)
[1] "123" "125"
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
0

This could help:

substring(text = as.numeric(gsub("[^\\d]+", "", a, perl=TRUE)),first = 1,last = 3)

[1] "123" "125"
Duck
  • 39,058
  • 13
  • 42
  • 84