creating sequence of dates for each group in r

别说谁变了你拦得住时间么 提交于 2019-11-28 12:27:01

You could use data.table to get the sequence of Dates from 'created_at' to '2015-07-12', grouped by the 'ID' column.

 library(data.table)
 setDT(df1)[, list(date=seq(created_at, as.Date('2015-07-12'), by='1 day')) , ID]

If you need an option with dplyr, use do

 library(dplyr)
 df1 %>% 
   group_by(ID) %>% 
   do( data.frame(., Date= seq(.$created_at,
                            as.Date('2015-07-12'), by = '1 day')))

If you have duplicate IDs, then we may need to group by row_number()

df1 %>%
    group_by(rn=row_number()) %>%
     do(data.frame(ID= .$ID, Date= seq(.$created_at,
          as.Date('2015-07-12'), by = '1 day'), stringsAsFactors=FALSE))

Update

Based on @Frank's commment, the new idiom for tidyverse is

library(tidyverse)
df1 %>%
  group_by(ID) %>% 
  mutate(d = list(seq(created_at, as.Date('2015-07-12'), by='1 day')), created_at = NULL) %>%
  unnest()

In the case of data.table

setDT(df1)[, list(date=seq(created_at, 
             as.Date('2015-07-12'), by = '1 day')), by = 1:nrow(df1)] 

data

df1 <- structure(list(ID = c("MUM-0001", "MUM-0002", "MUM-0003",
 "MUM-0004", 
 "MUM-0005", "MUM-0006"), created_at = structure(c(16176, 16084, 
16177, 16172, 16178, 16177), class = "Date")), .Names = c("ID", 
"created_at"), row.names = c(NA, -6L), class = "data.frame")
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!