1

Is there a way to sort data by date periods. For eg

myCurve <- data.table( Term=c('10y', '3y', '5y'), value = c(2, 1.25, 1.9))

I would like to get the data sorted by Term so that the results are like so

Term  Value
3y    1.25
5y    1.90
10y   2.00

Term could be d,w,m,y etc.

1 Answers1

3

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))
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks @akrun. The 'factor' method comes close but not complete. For eg 12w should come after 2m. But in the suggested solution 12w comes prior to 2m and hence not complete. – Balaji Subramanian Sep 06 '15 at 13:37
  • @BalajiSubramanian Can you try the updated code. Here, I am taking 'm' as 30 days and 'y' as 365 – akrun Sep 06 '15 at 13:44