使用两个数据框列应用seq.Date [英] Apply seq.Date using two dataframe columns
问题描述
我尝试使用
中的各种答案扩展序列在数据框中
,但没有任何尝试。
I tried to use the varies answers from Expanding a sequence in a data frame to my dataframe, but nothing I tried works.
样本数据
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
这里有3个代码试验-我都尝试了多种变体(例如,apply系列的众多成员),但都失败了(即给出了各种错误消息):
Here are three code trials - all of which I tried in numerous variants (e.g. verious members of the apply family), but all failed (i.e. giving diverse error messages):
代码示例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"))
推荐答案
您的问题是您给 seq.Date
提供了一个向量,该向量采用唯一值从
或到
。
Your problem is that you give a vector to seq.Date
which takes a unique value as from
or to
.
与您的想法相同apply调用应为:
On the same idea as your apply call it should be:
apply(df2,1,function(x) { seq.Date( as.Date(x['StartDate']), as.Date(x['EndDate']), by='days') } )
这将为您提供每个行序列的列表:
Which gives you a list with each row sequence:
[[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列。
To get your desired output, we should return id and NoDays columns too.
基本RI会做像这样:
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答案,因为您似乎正在使用此软件包。 / strike>在寻找dplyr示例时终于找到了骗子,投票关闭了。
I'll see if I can craft a correct dplyr answer as you seems to be using this package. Finally found a dupe when looking for dplyr examples, voted to close.
这篇关于使用两个数据框列应用seq.Date的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!