MLE
MLE

Reputation: 1043

insert rows between dates by group

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 wondering is there any easier way to achieve it.

# 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

Desired output

enter image description here

Upvotes: 6

Views: 1671

Answers (6)

Abdou
Abdou

Reputation: 13284

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

Upvotes: 2

roarkz
roarkz

Reputation: 831

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))

Upvotes: 2

BENY
BENY

Reputation: 323366

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

Upvotes: 13

Joel Alcedo
Joel Alcedo

Reputation: 312

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()

Upvotes: 1

www
www

Reputation: 39174

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

Upvotes: 2

myincas
myincas

Reputation: 1540

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)]

Upvotes: 2

Related Questions