Skip to content
Prev 369595 / 398503 Next

New var

Val
Thank you all for the useful suggestion. I did some of my homework.

library(data.table)
DFM <- read.table(header=TRUE, text='obs start end
1 2/1/2015   1/1/2017
2 4/11/2010  1/1/2011
3 1/4/2006   5/3/2007
4 10/1/2007  1/1/2008
5 6/1/2011   1/1/2012
6 10/5/2004 12/1/2004',stringsAsFactors = FALSE)
DFM

DFM$D =as.numeric(difftime(as.Date(DFM$end,format="%m/%d/%Y"),
as.Date(DFM$start,format="%m/%d/%Y"), units = "days"))
DFM

output.
     obs     start       end   D
1   1  2/1/2015  1/1/2017 700
2   2 4/11/2010  1/1/2011 265
3   3  1/4/2006  5/3/2007 484
4   4 10/1/2007  1/1/2008  92
5   5  6/1/2011  1/1/2012 214
6   6 10/5/2004 12/1/2004  57

My problem is how do I get the other new variables

obs     start       end   D  t1,t2,t3,t4, t5
1, 2/1/2015,  1/1/2017, 700,0,0,0,0,0
2, 4/11/2010, 1/1/2011, 265,0,0,1,-1,-1
3, 1/4/2006,  5/3/2007, 484,0,0,0,0,1
4, 10/1/2007, 1/1/2008, 92,1,-1,-1,-1,-1
5, 6/1/2011,  1/1/2012, 214,0,0,1,-1,-1
6, 10/15/2004,12/1/2004,47,1,-1,-1,-1,-1

Thank you again.
On Sat, Jun 3, 2017 at 12:13 AM, Bert Gunter <bgunter.4567 at gmail.com> wrote: