[datatable-help] Two short questions about the operation of data table

Frank S. f_j_rod at hotmail.com
Mon Sep 22 13:16:06 CEST 2014


Hello to everyone, Let's consider, just by way of example, the following date and data table: opening <- as.Date("1990-01-01")DT <- data.table(ID=c(1,2,3),    start=c("1985-01-01","1993-07-15","1993-05-17"),    end=c("1992-05-01","1997-02-25","2002-01-01"),    value=c(7.8, 3.2, 20.0)) FIRST QUESTION:  If I execute: DTNEW <- DT[ , {    if (all(start <= opening)){      result <- list(start, end, t.dif= unclass(round(difftime(end, start)/365.25,1)), value)      } else {      result <- list(start, end, t.dif= 20, value)     }    result}, by=ID]  Why can I not keep the column names?   ID                       t.dif     1:  1 1985-01-01 1992-05-01   7.3  7.82:  2 1993-07-15 1997-02-25  20.0  3.23:  3 1993-05-17 2002-01-01  20.0 20.0SECOND QUESTION: I would want to remove rows where t.dif=value in the final result. Then, I tried: DTNEW <- DT[ , {    if (all(start <= opening)){      result <- list(start, end,  t.dif= unclass(round(difftime(end, start)/365.25,1)), value)      } else {      result <- list(start, end,  t.dif= 20, value)     }    result[!(t.dif == value)]}, by=ID]  But R does not find the variable t.dif !! Thank you for your time to all of the members of the list!!               		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140922/caffacdc/attachment.html>


More information about the datatable-help mailing list