I have a string column with the length of a period, formated as "xx years yy months". I want to express these periods in months only, i.e. 12 * number of years + number of months.
A small example:
x = c("2 years 5 months", "10 years 10 months")
Here the desired result is 2 * 12 + 5 = 29, and 10 * 12 + 10 = 130 respectively.
I tried the substr function, but I didn't manage to handle the fact that month and year may be either one or two digits.
12 * as.numeric(substr(x, 1, 2)) + as.numeric(substr(x, 6, 7)))
And then I tried the sprintf as below, but it didn't give the intended result.
sprintf("%1.0f", x))