为r中的每个组创建日期序列 [英] creating sequence of dates for each group in r

查看:153
本文介绍了为r中的每个组创建日期序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据集,如下所示:

I have a dataset that looks like this:

      ID    created_at
MUM-0001    2014-04-16
MUM-0002    2014-01-14
MUM-0003    2014-04-17
MUM-0004    2014-04-12
MUM-0005    2014-04-18
MUM-0006    2014-04-17

我正在尝试引入全新的列开始日期和定义的最后一天之间的日期(例如,2015年7月12日)。我在 dplyr 中使用seq函数,但出现错误。

I am trying to introduce new column that would be all dates between start date and defined last day (say, 12th-july-2015). I used seq function in dplyr but getting an error.

data1 <- data1 %>%
         arrange(ID) %>%
         group_by(ID) %>%
         mutate(date = seq(as.Date(created_at), as.Date('2015-07-12'), by= 1))

我的错误得到的是:


错误:不兼容大小(453),期望1(组大小)或1

Error: incompatible size (453), expecting 1 (the group size) or 1

您能否在R中提出一些更好的方式来执行此任务?

Can you please suggest some better way to perform this task in R ?

推荐答案

您可以使用 data.table 将created_at的日期的序列 07-12',按ID列分组。

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]

如果您需要使用 dplyr 的选项,请使用 do

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






如果您有重复的ID,那么我们可能需要通过 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))

数据的情况下。表

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

这篇关于为r中的每个组创建日期序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆