I currently have an edge list in R that looks something like this:
我目前在R中有一个边缘列表,看起来像这样:
From To Start End
A B 1/1/2011 1/2/2011
A F 1/1/2011 1/1/2011
B G 1/2/2011 1/2/2011
C A 1/2/2011 1/6/2011
D C 1/2/2011 1/3/2011
What I would like to do is create a new edge list in R that looks something like this
我想要做的是在R中创建一个看起来像这样的新边列表
From To Time
A B 1/1/2011
A B 1/2/2011
A F 1/1/2011
B G 1/2/2011
C A 1/2/2011
C A 1/3/2011
C A 1/4/2011
C A 1/5/2011
C A 1/6/2011
D C 1/2/2011
D C 1/3/2011
In other words, I would like to duplicate the first two columns each row in the first table for each day between the start and end date, inclusive. What would be a good way to do this? Thanks so much for your help!
换句话说,我想在开始日期和结束日期之间的每一天(包括第一个表格)中复制第一个表格中每一行的前两列。这样做有什么好办法?非常感谢你的帮助!
1 个解决方案
#1
1
The Date
class has a seq
function (seq.Date
) that you can use, or you can use IDate
from data.table
:
Date类有一个你可以使用的seq函数(seq.Date),或者你可以使用data.table中的IDate:
df = read.table(text = 'From To Start End
A B 1/1/2011 1/2/2011
A F 1/1/2011 1/1/2011
B G 1/2/2011 1/2/2011
C A 1/2/2011 1/6/2011
D C 1/2/2011 1/3/2011', header = T)
library(data.table)
dt = data.table(df)
dt[, cbind(.SD, seq(as.Date(Start, '%m/%d/%Y'),
as.Date(End, '%m/%d/%Y'), 1)), by = list(Start,End)]
# or
dt[, cbind(.SD, seq(as.IDate(Start, '%m/%d/%Y'),
as.IDate(End, '%m/%d/%Y'), 1)), by = list(Start,End)]
# Start End From To V2
# 1: 1/1/2011 1/2/2011 A B 2011-01-01
# 2: 1/1/2011 1/2/2011 A B 2011-01-02
# 3: 1/1/2011 1/1/2011 A F 2011-01-01
# 4: 1/2/2011 1/2/2011 B G 2011-01-02
# 5: 1/2/2011 1/6/2011 C A 2011-01-02
# 6: 1/2/2011 1/6/2011 C A 2011-01-03
# 7: 1/2/2011 1/6/2011 C A 2011-01-04
# 8: 1/2/2011 1/6/2011 C A 2011-01-05
# 9: 1/2/2011 1/6/2011 C A 2011-01-06
#10: 1/2/2011 1/3/2011 D C 2011-01-02
#11: 1/2/2011 1/3/2011 D C 2011-01-03
#1
1
The Date
class has a seq
function (seq.Date
) that you can use, or you can use IDate
from data.table
:
Date类有一个你可以使用的seq函数(seq.Date),或者你可以使用data.table中的IDate:
df = read.table(text = 'From To Start End
A B 1/1/2011 1/2/2011
A F 1/1/2011 1/1/2011
B G 1/2/2011 1/2/2011
C A 1/2/2011 1/6/2011
D C 1/2/2011 1/3/2011', header = T)
library(data.table)
dt = data.table(df)
dt[, cbind(.SD, seq(as.Date(Start, '%m/%d/%Y'),
as.Date(End, '%m/%d/%Y'), 1)), by = list(Start,End)]
# or
dt[, cbind(.SD, seq(as.IDate(Start, '%m/%d/%Y'),
as.IDate(End, '%m/%d/%Y'), 1)), by = list(Start,End)]
# Start End From To V2
# 1: 1/1/2011 1/2/2011 A B 2011-01-01
# 2: 1/1/2011 1/2/2011 A B 2011-01-02
# 3: 1/1/2011 1/1/2011 A F 2011-01-01
# 4: 1/2/2011 1/2/2011 B G 2011-01-02
# 5: 1/2/2011 1/6/2011 C A 2011-01-02
# 6: 1/2/2011 1/6/2011 C A 2011-01-03
# 7: 1/2/2011 1/6/2011 C A 2011-01-04
# 8: 1/2/2011 1/6/2011 C A 2011-01-05
# 9: 1/2/2011 1/6/2011 C A 2011-01-06
#10: 1/2/2011 1/3/2011 D C 2011-01-02
#11: 1/2/2011 1/3/2011 D C 2011-01-03