[R] New var

Val valkremk at gmail.com
Sat Jun 3 05:49:11 CEST 2017


Hi all,

I have a data set with time interval and depending on the interval I want
to create 5 more variables . Sample data below

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/15/2004,12/1/2004

First, I want get  interval between the start date and end dates
(End-start).

 obs,  Start , end, datediff
1,2/1/2015,  1/1/2017, 700
2,4/11/2010, 1/1/2011, 265
3,1/4/2006,  5/3/2007, 484
4,10/1/2007, 1/1/2008, 92
5,6/1/2011,  1/1/2012, 214
6,10/15/2004,12/1/2004,47

Second. I want create 5 more variables  t1, t2, t3, t4 and  t5
The value of each variable is defined as follows
if datediff <   100 then  t1=1,  t2=t3=t4=t5=-1.
if datediff >= 100 and  < 200 then  t1=0, t2=1,t3=t4=t5=-1,
if datediff >= 200 and  < 300 then  t1=0, t2=0,t3=1,t4=t5=-1,
if datediff >= 300 and  < 400 then  t1=0, t2=0,t3=0,t4=1,t5=-1,
if datediff >= 400 and  < 500 then  t1=0, t2=0,t3=0,t4=0,t5=1,
if datediff >= 500 then  t1=0, t2=0,t3=0,t4=0,t5=0

The complete out put looks like as follow.
obs, start,         end,    datediff,   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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list