cune
cune

Reputation: 79

extract numerical suffixes from strings in R

I have this character vector:

variables <- c("ret.SMB.l1", "ret.mkt.l1", "ret.mkt.l4", "vix.l4", "ret.mkt.l5" "vix.l6", "slope.l11",  "slope.l12", "us2yy.l2")

Desired output:

> suffixes(variables)
[1] 1 1 4 4 5 6 11 12 2

In other words, I need a function that will return a numeric vector showing the suffixes (each of which be 1 or 2 digits long). Note, I need something that can work with a much larger number of strings which may or may not have numbers somewhere the middle. The numerical suffixes range from 1 to 99.

Many thanks

Upvotes: 0

Views: 517

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

You could use sub function.

> variables <- c("ret.SMB.l1", "ret.mkt.l1", "ret.mkt.l4", "vix.l4", "ret.mkt.l5" ,"vix.l6", "slope.l11",  "slope.l12", "us2yy.l2")
> sub(".*\\D", "", variables)
[1] "1"  "1"  "4"  "4"  "5"  "6"  "11" "12" "2" 

.*\\D matches all the characters from the start upto the last non-digit character. Replacing those matched characters with an empty string will give you the desired output.

Upvotes: 3

A5C1D2H2I1M1N2O1R2T1
A5C1D2H2I1M1N2O1R2T1

Reputation: 193517

Just use gsub:

> gsub(".*?([0-9]+)$", "\\1", variables)
[1] "1"  "1"  "4"  "4"  "5"  "6"  "11" "12" "2" 

Wrap it in as.numeric if you want the result as a number.

Upvotes: 5

Related Questions