insert rows between dates by group

前端 未结 6 1800
半阙折子戏
半阙折子戏 2021-02-10 02:42

I want to insert rows between two dates by group. My way of doing it is so complicated that I insert missing values by last observation carry forwards and then merge. I was wond

相关标签:
6条回答
  • 2021-02-10 02:59

    By using dplyr and tidyr :)(one line solution )

    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
    
    0 讨论(0)
  • 2021-02-10 03:01

    you can try this

    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)]
    
    0 讨论(0)
  • 2021-02-10 03:12

    We can use the tidyverse to achieve this task.

    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
    
    0 讨论(0)
  • 2021-02-10 03:13

    The simplest way that I have found to do this is with the padr library.

    library(padr)
    dt_padded <- pad(dt, group = "user", by = "date") %>%
      replace_na(list(dummy=0))
    
    0 讨论(0)
  • 2021-02-10 03:13

    A Base R (not quite as elegant) solution:

    # 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
    
    0 讨论(0)
  • 2021-02-10 03:16

    Assuming your data is called df1, and you want to add dates between two days try this:

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

    If you're simply trying to add a new record, I suggest using rbind.

    rbind()
    
    0 讨论(0)
提交回复
热议问题