[datatable-help] Subsetting a data table and add a new column in one step

Frank S. f_j_rod at hotmail.com
Tue Jul 22 16:49:33 CEST 2014



Hello everyone.  I’ve the following data table:

 

DT <- data.table(id=1:5,

                born=as.Date(c("1939-10-28","1943-02-26","1946-03-09","1947-05-19","1932-04-03")),

                start=as.Date(c("2012-01-01","1980-07-15","1998-10-28","2011-10-28","2010-10-28")),

                end=as.Date(c("2012-05-01","2014-02-01","2012-10-20","2013-10-15","2012-08-25")))

>DT

   id      
born      start        end

1:  1 1939-10-28 2012-01-01 2012-05-01

2:  2 1943-02-26 1980-07-15 2014-02-01

3:  3 1946-03-09 1998-10-28 2012-10-20

4:  4 1947-05-19 2011-10-28 2013-10-15

5:  5 1932-04-03 2010-10-28 2012-08-25

I
would like to be able to keep only those subjects whose “start” date  is previous to “2010-01-01” date, and then
calculatethe age they were at 2010-01-01 in a newDT:

    id      
born      start        end    
age

2:  2 1943-02-26 1980-07-15 2014-02-01    66.8

3:  3 1946-03-09 1998-10-28 2012-10-20    63.8

 

I
have:

 

newDT <- DT[, if(start <= as.Date("2010-01-01"))
{

list(c(id, born,
start, end, age=unclass(round(difftime(Apertura, born)/365.25,1))))

} , by=c('id','born','start','end')]

 But it appears an error message!  Can anyone please help me with this? Thank you!  

 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140722/9441ad47/attachment.html>


More information about the datatable-help mailing list