java年月日排序_R:按日期排序(按年份,按月)

在tidyverse(w / lubridate添加):

library(tidyverse)

library(lubridate)

dfYrMon

df1 %>%

mutate(date = parse_date_time(month, "my"),

year = year(date),

month = month(date)

) %>%

arrange(year, month) %>%

select(date, year, month, result)

有了数据:

df1

result = c(96.13636, 96.4, 94, 97.92857, 95.75, 98.66667, 97.78947))

会得到这个'数据帧':

#A tibble:7 x 4

日期年度结果

1 2017-11-01 2017 11 98.66667

2 2017-12-01 2017 12 97.78947

3 2018-01-01 2018 1 96.13636

4 2018-02-01 2018 2 96.40000

5 2018-03-01 2018 3 94.00000

6 2018-04-01 2018 4 97.92857

7 2018-05-01 2018 5 95.75000

使您的数据值成为原子(年份在其自己的列中,月份在其自己的列中)通常会提高操作的便利性 .

或者如果你想使用基本R日期操作而不是lubridate:

library(tidyverse)

dfYrMon_base

df1 %>%

mutate(date = as.Date(paste("01/", month, sep = ""), "%d/%m/%Y"),

year = format(as.Date(date, format="%d/%m/%Y"),"%Y"),

month = format(as.Date(date, format="%d/%m/%Y"),"%m")

) %>%

arrange(year, month) %>%

select(date, year, month, result)

dfYrMon_base

请注意创建的数据类型 .

#A tibble:7 x 4

日期年度结果

1 2017-11-01 2017 11 98.66667

2 2017-12-01 2017 12 97.78947

3 2018-01-01 2018 01 96.13636

4 2018-02-01 2018 02 96.40000

5 2018-03-01 2018 03 94.00000

6 2018-04-01 2018 04 97.92857

7 2018-05-01 2018 05 95.75000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值