Sorting data frame based on month-year time format

后端 未结 6 2117
孤城傲影
孤城傲影 2021-01-03 06:29

I\'m struggling with something very basic: sorting a data frame based on a time format (month-year, or, “%B-%y” in this case). My goal is to calculate various monthly statis

6条回答
  •  攒了一身酷
    2021-01-03 06:49

    It looks like the main problem is how to sort a sequence of Month-Year strings chronologically. The easiest way is to pre-pend a "01" at the beginning of each Month-Year string and sort them as regular dates. So take your final data-frame Tmp09Totals, and do this:

    monYear <- rownames(Tmp09Totals)
    sortedMonYear <- format(sort( as.Date( paste('01-', monYear, sep = ''),
                                           '%d-%B-%y')), 
                           '%B-%y')
    Tmp09Totals[ sortedMonYear, , drop = FALSE]
    

提交回复
热议问题