r语言 - 将月份转换为跨越4年的连续数字



我有

> table(a)
a
2017-1 2017-10 2017-11  2017-2  2017-4  2017-5  2017-6  2017-7  2017-8  2017-9  2018-1 
4       3       3       4       3       4       4       3       1       1       1 
2018-10  2018-2  2018-3  2018-4  2018-5  2018-6  2018-7  2019-1 2019-10 2019-11 2019-12 
2       2       3       1       1       1       1       3       1       2       4 
2019-2  2019-3  2019-4  2019-5  2019-6  2019-7  2019-8  2020-1 2020-10 2020-11 2020-12 
1       1       6       2       1       7       3       1       3       3       3 
2020-2  2020-3  2020-4  2020-5  2020-6  2020-7  2020-8  2020-9 
2       1       1       2       2       1       1       7

对应yyyy-m,从2017-12020-12,即48个月。我需要连续计算每个月,2017-1对应1,2020-12对应48

我跟踪了这个帖子,但是没有解决我的问题。

我更喜欢dplyr的解决方案,我尝试了:

arrange(a, date) %>% mutate(cons_date = dense_rank(date))

但显然有一些错误-a$date没有正确排列,例如2017-2存在,但在2017-10之后:

> head(arrange(a, date) %>% mutate(cons_date = dense_rank(date)), 10)
date cons_date
1   2017-1         1
2   2017-1         1
3   2017-1         1
4   2017-1         1
5  2017-10         2
6  2017-10         2
7  2017-10         2
8  2017-11         3
9  2017-11         3
10 2017-11         3

数据
a <- structure(list(date = c("2018-3", "2019-8", "2017-1", "2020-11", 
"2018-6", "2019-7", "2018-3", "2017-6", "2017-1", "2017-5", "2018-4", 
"2019-5", "2017-11", "2017-11", "2017-10", "2019-11", "2019-6", 
"2019-7", "2019-5", "2020-5", "2017-5", "2019-1", "2017-7", "2019-4", 
"2019-12", "2017-5", "2020-10", "2020-5", "2020-7", "2019-11", 
"2017-9", "2018-2", "2017-4", "2017-2", "2017-2", "2020-2", "2019-4", 
"2020-9", "2017-10", "2017-6", "2018-3", "2017-5", "2017-7", 
"2020-11", "2019-7", "2020-1", "2019-4", "2019-10", "2020-12", 
"2020-3", "2020-6", "2020-11", "2019-4", "2020-6", "2019-4", 
"2018-10", "2017-2", "2020-12", "2019-4", "2018-7", "2019-7", 
"2017-10", "2020-10", "2017-1", "2017-4", "2017-2", "2020-9", 
"2019-1", "2020-9", "2020-8", "2018-1", "2020-2", "2019-7", "2017-6", 
"2020-10", "2019-7", "2017-11", "2018-10", "2019-1", "2018-2", 
"2020-9", "2019-12", "2017-4", "2019-8", "2020-4", "2017-7", 
"2020-9", "2020-12", "2019-2", "2020-9", "2017-1", "2019-12", 
"2019-7", "2018-5", "2019-8", "2017-6", "2020-9", "2019-12", 
"2017-8", "2019-3")), row.names = c(NA, -100L), class = "data.frame")

它不是Date类,因此,顺序将是字母数字。我们可以通过paste('01')转换为Date类,或者通过zoo(as.yearmon)转换为年度类

library(zoo)
library(dplyr)
a %>%
arrange(as.yearmon(date)) %>%
mutate(cons_date = dense_rank(as.yearmon(date))) %>% 
slice_head(n = 10)

与产出

#      date cons_date
#1  2017-1         1
#2  2017-1         1
#3  2017-1         1
#4  2017-1         1
#5  2017-2         2
#6  2017-2         2
#7  2017-2         2
#8  2017-2         2
#9  2017-4         3
#10 2017-4         3

关于OP对缺少'date'的关注,如果OP想跳过缺少的索引,那么我们可以使用match

a %>% 
mutate(date1 = as.yearmon(date)) %>%
arrange(date1) %>% 
mutate(cons_date = match(date1, as.yearmon(seq(min(as.Date(date1)),
max(as.Date(date1)), by = '1 month')))) %>% 
select(-date1) %>% 
slice_head(n = 10)
#      date cons_date
#1  2017-1         1
#2  2017-1         1
#3  2017-1         1
#4  2017-1         1
#5  2017-2         2
#6  2017-2         2
#7  2017-2         2
#8  2017-2         2
#9  2017-4         4
#10 2017-4         4

最新更新