如何按组填写范围内的缺失日期

low*_*rul 2 r sqldf dplyr

我有一data.frame组和日期.如何填写每组最小 - 最大日期范围内的所有缺失日期?

理想情况下我会这样做dplyr.但最终,我只想用尽可能少的(可读)代码行来有效地完成这项工作.以下是一个最小的例子.我实际上有很多日期和小组.我的两种方法看起来都很难看.必须有更好的方法,对吗?

#### setup ####

library(sqldf)
library(dplyr)
df <- data.frame(the_group = rep(LETTERS[1:2], each=3), date = Sys.Date() + c(0:2, 1:3), stringsAsFactors = F) %>%
  tbl_df() %>%
  slice(-2) # represents that I may be missing data in a range!

#### dplyr approach with cross join dummy ####
full_seq <- data.frame(cross_join_dummy = 1, date = seq.Date(from=min(df$date), to=max(df$date), by = "day"))

range_by_group <- df %>%
  group_by(the_group) %>%
  summarise(min_date = min(date), max_date = max(date)) %>%
  ungroup() %>%
  mutate(cross_join_dummy = 1)

desired <- range_by_group %>%
  inner_join(full_seq, by="cross_join_dummy") %>%
  filter(date >= min_date, date <= max_date) %>%
  select(the_group, date)

#### sqldf approach ####
full_seq <- data.frame(date = as.character(seq.Date(from=min(df$date), to=max(df$date), by="day")))

df <- df %>%
  mutate(date = as.character(date))

range_by_group <- sqldf("
                  SELECT the_group, MIN(date) AS min_date, MAX(date) AS max_date
                  FROM df
                  GROUP BY the_group
                  ")

desired <- sqldf("
            SELECT rbg.the_group, fs.date
            FROM range_by_group rbg
            JOIN full_seq fs
              ON fs.date BETWEEN rbg.min_date AND rbg.max_date
            ")
Run Code Online (Sandbox Code Playgroud)

G. *_*eck 5

1)没有包裹 - 由

这不使用任何包.by分割df通过df$the_group,然后在每一个执行指定的操作. do.call("rbind", ...)将各组重新组合在一起.

seq_date <- function(x) seq(min(x), max(x), by = "day")
do.call("rbind", by(df, df$the_group, with, 
  data.frame(the_group = the_group[1], date = seq_date(date))))
Run Code Online (Sandbox Code Playgroud)

2)data.table这是一个使用data.table的解决方案. seq_date来自(1)

library(data.table)

dt <- as.data.table(df)
dt[, list(date = seq_date(date)), by = the_group]
Run Code Online (Sandbox Code Playgroud)

3)tidyverse 这使用map_dfpurrr将公式表示法中给出的函数应用于组,并将结果放在一起形成数据框.data_frame来自tibble包.seq_date来自(1).

library(tidyverse)

df %>% 
   split(.$the_group) %>% 
   map_df(~ data_frame(the_group = .$the_group[1], date = seq_date(.$date)))
Run Code Online (Sandbox Code Playgroud)

4)tapply

4a)tapply - tidyr/reshape2 seq_date来自(1).

library(tidyr)
library(reshape2)

df %>%
   { tapply(.$date, .$the_group, seq_date, simplify = FALSE) } %>%
   melt %>%
   unnest
Run Code Online (Sandbox Code Playgroud)

4b)tapply - 没有包 最后一行将输出组合在一起,tapply避免了任何包的需要. seq_date来自(1).

ta <- tapply(df$date, df$the_group, seq_date, simplify = FALSE)
data.frame(the_group = rep(names(ta), lengths(ta)), date = do.call("c", ta))
Run Code Online (Sandbox Code Playgroud)

4C)tapply -晶格我们可以用格子包的make.groupsta从(4B).网格预先安装了R,因此不需要安装任何其他软件包.不幸的是make.groups删除了Date class属性,所以我们必须把它放回去.还make.groups使用whichdata列名称,以便我们修复列名称.

library(lattice)
with(do.call("make.groups", ta), 
  data.frame(the_group = which, date = structure(data, class = "Date")))
Run Code Online (Sandbox Code Playgroud)

4D)tapply -没有软件包-叠 我们可以使用stack转换ta从(4B),以提供我们除去所期望的形式"Date"一流.然后申请后stack我们可以恢复"Date"班级. stack使用我们替换的硬编码列名称setNames.

stack_dates <- function(x) 
  transform(stack(lapply(x, as.vector)), values = structure(values, class = "Date"))
setNames(stack_dates(ta)[2:1], c("the_group", "date"))
Run Code Online (Sandbox Code Playgroud)