使用两个数据框列应用seq.Date

时间:2016-02-03 08:48:28

标签: r date sequence

我尝试使用各种答案 Expanding a sequence in a data frame 到我的数据框架,但没有尝试过。

样本数据

library(dplyr)    
p1 <- c(1:5)
p2 <- as.Date(c("2013-01-01","2013-01-22","2014-02-01","2014-05-12","2015-02-22"))
p3 <- as.Date(c("2013-01-11","2013-01-30","2014-02-20","2014-05-22","2015-02-28"))
p4 <- c(11,9,20,11,7)
df2 <- data_frame(p1,p2,p3,p4)
names(df2) <- c("ID", "StartDate", "EndDate", "NoDays")
df2

期望的结果

ID  datelist    NoDays
1   2013-01-01   1
1   2013-01-02   1 
1   2013-01-03   1
etc..
1   2013-01-10   1
1   2013-01-11   1
2   2013-01-22   1
2   2013-01-23   1
etc.
2   2013-01-28   1
2   2013-01-29   1
2   2013-01-30   1

以下是三个代码试用 - 我尝试了多种变体(例如应用系列的真实成员),但都失败了(即提供了不同的错误消息):

代码示例1

datelist <- seq.Date(from = df2$StartDate, to=df2$StartDate, by="days")

代码示例2

datelist <- seq.Date(from = df2$StartDate, by="days", length.out = df2$NoDays)

代码示例2

datelist <- apply(df2, 1, seq.Date(from = df2$StartDate, to=df2$StartDate, by="days"))

2 个答案:

答案 0 :(得分:4)

您的问题是,您向seq.Date提供了一个向量,该向量的唯一值为fromto

与申请电话相同的想法应该是:

apply(df2,1,function(x) { seq.Date( as.Date(x['StartDate']), as.Date(x['EndDate']), by='days') } )

它为您提供了每个行序列的列表:

[[1]]
 [1] "2013-01-01" "2013-01-02" "2013-01-03" "2013-01-04" "2013-01-05" "2013-01-06" "2013-01-07" "2013-01-08" "2013-01-09"
[10] "2013-01-10" "2013-01-11"

[[2]]
[1] "2013-01-22" "2013-01-23" "2013-01-24" "2013-01-25" "2013-01-26" "2013-01-27" "2013-01-28" "2013-01-29" "2013-01-30"

[[3]]
 [1] "2014-02-01" "2014-02-02" "2014-02-03" "2014-02-04" "2014-02-05" "2014-02-06" "2014-02-07" "2014-02-08" "2014-02-09"
[10] "2014-02-10" "2014-02-11" "2014-02-12" "2014-02-13" "2014-02-14" "2014-02-15" "2014-02-16" "2014-02-17" "2014-02-18"
[19] "2014-02-19" "2014-02-20"

[[4]]
 [1] "2014-05-12" "2014-05-13" "2014-05-14" "2014-05-15" "2014-05-16" "2014-05-17" "2014-05-18" "2014-05-19" "2014-05-20"
[10] "2014-05-21" "2014-05-22"

[[5]]
[1] "2015-02-22" "2015-02-23" "2015-02-24" "2015-02-25" "2015-02-26" "2015-02-27" "2015-02-28"

要获得所需的输出,我们也应该返回id和NoDays列。

在基地R我会这样做:

getDfForDates <- function(row) {
  dseq <- seq.Date( as.Date(row['StartDate']), as.Date(row['EndDate']), by='days')
  data.frame( ID=row['ID'], datelist=dseq, NoDays=1)
}

rbindlist(
  apply(df2,1,function(x) { 
    getDfForDates(x)
  } )
)

data.table包的另一个解决方案是:

setDT(df2)
df2[, list(datelist=seq.Date( StartDate, EndDate, by='days'), NoDays=1), by=ID]

如果我没有错过任何一点,两者都会得到理想的结果。

我会看看我是否可以制作正确的dplyr答案,因为你似乎正在使用这个软件包。最后在寻找dplyr示例时发现了一个骗局,投票结束。

答案 1 :(得分:3)

我们可以使用data.table轻松完成此操作。转换&#39; data.frame&#39;到&#39; data.table&#39; (setDT(df2),如果“ID”是唯一的,则按“ID&#39;”进行分组,然后获取“{1}}”&#39;“开始日期”&#39;到&#39; EndDate&#39; seq&#39; ID&#39;。

by

如果我们需要在library(data.table) res <- setDT(df2)[,list(datelist=seq(StartDate, EndDate, by='1 day'), NoDays = 1) , by = ID] head(res) # ID datelist NoDays #1: 1 2013-01-01 1 #2: 1 2013-01-02 1 #3: 1 2013-01-03 1 #4: 1 2013-01-04 1 #5: 1 2013-01-05 1 #6: 1 2013-01-06 1 中执行此操作,我们可能需要dplyr,因为do不支持此类操作

mutate