gpt4 book ai didi

r - 按组展开日期

转载 作者:行者123 更新时间:2023-12-01 11:00:49 24 4
gpt4 key购买 nike

我有这样的数据:

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

我正在尝试按组扩展日期列,以便以这样的方式结束:

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 管道中执行此操作?

最佳答案

只需安装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

关于r - 按组展开日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46614698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com