date - Create season variable in R -


my season time oct 1st march 31st of following year. how create dummy variable season see person exposed in , out

 df <- data.frame(id= c(1:6),               drug = c("a","c","a","a","b","a"),              start = c("01/01/2009","07/10/2010","10/10/2009","03/01/2011","03/01/2012","04/12/2010"),              end=c("09/10/2009","04/20/2011","07/20/1010","01/01/2012","04/01/2013","09/30/2011")) 

my output:

   id drug      start        end season 1   1    01/01/2009 09/10/2009      1 2   1    01/01/2009 09/10/2009      0 3   2    c 07/10/2010 04/20/2011      0 4   2    c 07/10/2010 04/20/2011      1 5   2    c 07/10/2010 04/20/2011      0 6   3    10/10/2009 07/20/1010      1 7   3    10/10/2009 07/20/1010      0 8   3    10/10/2009 07/20/1010      1 9   4    b 03/01/2011 01/01/2012      1 10  4    b 03/01/2011 01/01/2012      0 11  4    b 03/01/2011 01/01/2012      1 12  5    03/01/2012 04/01/2013      1 13  5    03/01/2012 04/01/2013      0 14  5    03/01/2012 04/01/2013      1 15  5    03/01/2012 04/01/2013      0 16  6    04/12/2010 09/30/2011      0 

id 1: started 01/01 , end 09/10.

[01/01, 03/31] =1  [03/31,09/10] = 0 

id 2: started 07/10 , end 04/20. check

[07/10, 10/01] = 0  [10/01,03/31] = 1  [03/31, 04/20] = 0 

id5 started 03/01 , ended 04/01

[03/01, 03/31]= 1  [03/31, 10/01] = 0  [10/01, 03/31] = 1  [03/31, 04/01] = 0 

i think got exposedin , exposedout correct code below (note: need add 'stringsasfactors = false' when create data frame). however, didn't quite have time work out additional sum of entire seasons covered -- adding column date/time function takes account total treatment time.

df$start <- as.date(df$start, format = '%m/%d/%y') df$end <- as.date(df$end, format = '%m/%d/%y') df$seasonin <- 274 # 275 in leap years df$seasonout <- 90 # 91 in leap years df$exposedin <- as.integer(as.posixlt(df$start)$yday >= df$seasonin |  as.posixlt(df$start)$yday < df$seasonout) df$exposedout <- as.integer(as.posixlt(df$end)$yday >= df$seasonin |  as.posixlt(df$end)$yday < df$seasonout) 

hope @ least helps some.


Comments

Popular posts from this blog

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -

python - Error while using APScheduler: 'NoneType' object has no attribute 'now' -