How do I add duplicate rows to a data frame in R? -
i have edge list in r looks this:
from start end b 1/1/2011 1/2/2011 f 1/1/2011 1/1/2011 b g 1/2/2011 1/2/2011 c 1/2/2011 1/6/2011 d c 1/2/2011 1/3/2011
what create new edge list in r looks this
from time b 1/1/2011 b 1/2/2011 f 1/1/2011 b g 1/2/2011 c 1/2/2011 c 1/3/2011 c 1/4/2011 c 1/5/2011 c 1/6/2011 d c 1/2/2011 d c 1/3/2011
in other words, duplicate first 2 columns each row in first table each day between start , end date, inclusive. way this? help!
the date
class has seq
function (seq.date
) can use, or can use idate
data.table
:
df = read.table(text = 'from start end b 1/1/2011 1/2/2011 f 1/1/2011 1/1/2011 b g 1/2/2011 1/2/2011 c 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)), = list(start,end)] # or dt[, cbind(.sd, seq(as.idate(start, '%m/%d/%y'), as.idate(end, '%m/%d/%y'), 1)), = list(start,end)] # start end v2 # 1: 1/1/2011 1/2/2011 b 2011-01-01 # 2: 1/1/2011 1/2/2011 b 2011-01-02 # 3: 1/1/2011 1/1/2011 f 2011-01-01 # 4: 1/2/2011 1/2/2011 b g 2011-01-02 # 5: 1/2/2011 1/6/2011 c 2011-01-02 # 6: 1/2/2011 1/6/2011 c 2011-01-03 # 7: 1/2/2011 1/6/2011 c 2011-01-04 # 8: 1/2/2011 1/6/2011 c 2011-01-05 # 9: 1/2/2011 1/6/2011 c 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
Comments
Post a Comment