I noticed that instead of the simple example in the OP's post created with just only 'year', there may be cases where 'day', 'week' and 'month' (represented by 'd', 'w', 'm' as suffix) would also be present in the original data. We could convert the strings to days. We separate the numeric from the non-numeric characters in the 'Term', convert the 'non-numeric' to factor
, specify the levels
in days such that we convert 'y' to 365, 'm' to '30', 'w' to '7' and 'd' as 1 similarly the numeric part is converted using as.numeric
, multiply, and then order
.
myCurve1[order(as.numeric(sub('\\D+', '', Term))*
as.numeric(as.character(factor(sub('\\d+', '', Term),
levels=c('d', 'w', 'm', 'y'), labels=c(1, 7, 30, 365)))))]
# Term Value
#1: 5d 2.00
#2: 12d 2.40
#3: 2w 2.10
#4: 2m 4.30
#5: 12w 4.20
#6: 7m 7.50
#7: 3y 1.90
#8: 5y 2.50
#9: 10y 1.25
data
myCurve1 <- data.table(Term=c('10y', '3y', '5d', '2w', '12d', '12w', '7m',
'5y', '2m'), Value=c(1.25, 1.9, 2, 2.1, 2.4, 4.2, 7.5, 2.5, 4.3))