在R中排序月份 [英] Sorting months in R

查看:263
本文介绍了在R中排序月份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想排序月份名称。当我使用 strptime 函数时,返回错误,因为属性值只包含月份名称。当我使用 sort 函数时,这些月份按字母顺序排列。

解决方案

您可以随时将数据转换为一个因素。例如,假设我们有

  x = c(1月,2月,3月,1月 

然后转换为一个因素,我们有:

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

在排序中给出:

  R> sort(x_fac)
[1] 1月1月2月3月
12级别:1月2月3月4月5月6月7月8月12月


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 function, the months are sorted alphabetically.

解决方案

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

这篇关于在R中排序月份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆