按组展开日期

时间:2017-10-06 22:20:52

标签: r date dplyr lubridate

我有这样的数据:

structure(list(group = c("A", "A", "A", "A", "B", "B"), date = structure(c(1262304000, 
1267401600, 1288569600, 1293840000, 1328054400, 1333238400), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), freq = c(5, 1, 20, 6, 2, 8)), .Names = c("group", 
"date", "freq"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-6L))

> df
# A tibble: 6 x 3
  group       date  freq
  <chr>     <dttm> <dbl>
1     A 2010-01-01     5
2     A 2010-03-01     1
3     A 2010-11-01    20
4     A 2011-01-01     6
5     B 2012-02-01     2
6     B 2012-04-01     8

我正在尝试按组扩展日期col,以便最终得到:

structure(list(group = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "B", "B", "B"), date = structure(c(1262304000, 
1264982400, 1267401600, 1270080000, 1272672000, 1275350400, 1277942400, 
1280620800, 1283299200, 1285891200, 1288569600, 1291161600, 1293840000, 
1328054400, 1330560000, 1333238400), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), freq = c(5, 0, 1, 0, 0, 0, 0, 0, 0, 0, 20, 
0, 6, 2, 0, 8)), .Names = c("group", "date", "freq"), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

> df_out
# A tibble: 16 x 3
   group       date  freq
   <chr>     <dttm> <dbl>
 1     A 2010-01-01     5
 2     A 2010-02-01     0
 3     A 2010-03-01     1
 4     A 2010-04-01     0
 5     A 2010-05-01     0
 6     A 2010-06-01     0
 7     A 2010-07-01     0
 8     A 2010-08-01     0
 9     A 2010-09-01     0
10     A 2010-10-01     0
11     A 2010-11-01    20
12     A 2010-12-01     0
13     A 2011-01-01     6
14     B 2012-02-01     2
15     B 2012-03-01     0
16     B 2012-04-01     8

增量应为月,频率应为0.如何在dplyr管道中执行此操作?

1 个答案:

答案 0 :(得分:5)

只需安装padr

即可
library(padr)
pad(df,'month', group = "group", by = "date") %>%
    replace_na(list(freq=0))


# A tibble: 16 x 3
         date group  freq
 *     <date> <chr> <dbl>
 1 2010-01-01     A     5
 2 2010-02-01     A     0
 3 2010-03-01     A     1
 4 2010-04-01     A     0
 5 2010-05-01     A     0
 6 2010-06-01     A     0
 7 2010-07-01     A     0
 8 2010-08-01     A     0
 9 2010-09-01     A     0
10 2010-10-01     A     0
11 2010-11-01     A    20
12 2010-12-01     A     0
13 2011-01-01     A     6
14 2012-02-01     B     2
15 2012-03-01     B     0
16 2012-04-01     B     8