按组在日期之间插入行

时间:2017-09-01 01:16:40

标签: r date dataframe

我想按组在两个日期之间插入行。我这样做的方式是如此复杂,以至于我通过最后一次观察结转并然后合并来插入缺失值。我想知道有没有更简单的方法来实现它。

# sample data 
user<-c("A","A","B","B","B") 
dummy<-c(1,1,1,1,1)
date<-as.Date(c("2017/1/3","2017/1/6","2016/5/1","2016/5/3","2016/5/5"))
dt<-data.frame(user,dummy,date) 

  user dummy       date
1    A     1 2017-01-03
2    A     1 2017-01-06
3    B     1 2016-05-01
4    B     1 2016-05-03
5    B     1 2016-05-05

所需的输出

enter image description here

6 个答案:

答案 0 :(得分:7)

使用dplyrtidyr:)(一行解决方案)

library(dplyr)
library(tidyr)
dt %>% group_by(user) %>% complete(date=full_seq(date,1),fill=list(dummy=0))
# A tibble: 9 x 3
# Groups:   user [2]
    user       date dummy
  <fctr>     <date> <dbl>
1      A 2017-01-03     1
2      A 2017-01-04     0
3      A 2017-01-05     0
4      A 2017-01-06     1
5      B 2016-05-01     1
6      B 2016-05-02     0
7      B 2016-05-03     1
8      B 2016-05-04     0
9      B 2016-05-05     1

答案 1 :(得分:2)

你可以试试这个

library(data.table)
setDT(dt)
tmp <- dt[, .(date = seq.Date(min(date), max(date), by = '1 day')), by = 
'user']
dt <- merge(tmp, dt, by = c('user', 'date'), all.x = TRUE)
dt[, dummy := ifelse(is.na(dummy), 0, dummy)]

答案 2 :(得分:2)

我们可以使用tidyverse来完成此任务。

library(tidyverse)

dt2 <- dt %>%
  group_by(user) %>%
  do(date = seq(from = min(.$date), to = max(.$date), by = 1)) %>%
  unnest() %>%
  left_join(dt, by = c("user", "date")) %>%
  replace_na(list(dummy = 0)) %>%
  select(colnames(dt))

dt2
# A tibble: 9 x 3
    user dummy       date
  <fctr> <dbl>     <date>
1      A     1 2017-01-03
2      A     0 2017-01-04
3      A     0 2017-01-05
4      A     1 2017-01-06
5      B     1 2016-05-01
6      B     0 2016-05-02
7      B     1 2016-05-03
8      B     0 2016-05-04
9      B     1 2016-05-05

答案 3 :(得分:2)

我发现这样做的最简单方法是使用padr库。

library(padr)
dt_padded <- pad(dt, group = "user", by = "date") %>%
  replace_na(list(dummy=0))

答案 4 :(得分:2)

A Base R(不太优雅)解决方案:

# Data
user<-c("A","A","B","B","B") 
dummy<-c(1,1,1,1,1)
date<-as.Date(c("2017/1/3","2017/1/6","2016/5/1","2016/5/3","2016/5/5"))
df1 <-data.frame(user,dummy,date)

# Solution
do.call(rbind, lapply(split(df1, df1$user), function(df) {
  dff <- data.frame(user=df$user[1], dummy=0, date=seq.Date(min(df$date), max(df$date), 'day'))
  dff[dff$date %in% df$date, "dummy"] <- df$dummy[1]
  dff
}))


# user dummy date      
# A    1     2017-01-03
# A    0     2017-01-04
# A    0     2017-01-05
# A    1     2017-01-06
# B    1     2016-05-01
# B    0     2016-05-02
# B    1     2016-05-03
# B    0     2016-05-04
# B    1     2016-05-05

答案 5 :(得分:1)

假设您的数据名为df1,并且您想要在两天之间添加日期,请尝试以下方法:

library(dplyr)
df2 <- seq.Date(as.Date("2015-01-03"), as.Date("2015-01-06"), by ="day")
left_join(df2, df1)

如果您只是想添加新记录,我建议使用rbind。

rbind()