Sorting months in R

后端 未结 2 2022
独厮守ぢ
独厮守ぢ 2021-02-13 12:29

I want to sort month names. When I use the strptime function it returns an error as the attribute values only contains month names. When I use the sort

2条回答
  •  Happy的楠姐
    2021-02-13 13:06

    You could always convert your data to a factor. For example, suppose we have

    x = c("January", "February", "March", "January")  
    

    then to convert to a factor, we have:

    x_fac = factor(x, levels = month.name)
    

    which on sorting gives:

    R> sort(x_fac)
    [1] January  January  February March   
    12 Levels: January February March April May June July August ... December
    

提交回复
热议问题