'Converting maturities to numeric format

I am trying to make a function to replace weird looking maturities into numbers:

For a reproducable example: the following block contains the data.

dict <- c("ON","TN","1W","1M","2M","3M","6M","9M","1Y","1Y3M","1Y6M","1Y9M","2Y","2Y3M", "2Y6M","2Y9M","3Y","3Y3M","3Y6M","3Y9M","4Y","4Y3M","4Y6M","4Y9M","5Y","5Y3M","5Y6M","5Y9M","6Y","6Y3M","6Y6M","6Y9M","7Y","7Y3M","7Y6M","7Y9M","8Y","8Y3M","8Y6M","8Y9M","9Y","9Y3M","9Y6M","9Y9M","10Y","11Y","12Y","13Y","14Y","15Y","20Y","25Y","30Y","40Y","50Y")

I tried to think about it as follows:

  • make a sub() function for the months (extract everything before M and after Y). Divide by 12.
  • Then add this value to another sub that I call to extract the years.
  • And then I guess a similar one for 0N (overnight so = 0) & TN (tomorrow next, so 1/365) although I would work with an if function for those? What do you think is most handy?

I was running this code. It correctly extracts the values after Y. Although it does not work for 10Y 11Y 12Y 13Y 14Y 15Y 20Y 25Y... Anyone that can provide support?

sub("*.Y", "", dict)

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source