[datatable-help] Two short questions about the operation of data table
Arunkumar Srinivasan
aragorn168b at gmail.com
Mon Sep 22 13:32:02 CEST 2014
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?
When we do:
DT[, list(x, y), by=z]
the j-expression returns an unnamed list. But we understand it as a straightforward list() scenario and extract the symbols and assign them as names in output/result.
But what happens within { ... } is more complicated and therefore is hard to extract the names to set names in result.
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 !!
But result is a list. You should be doing result[!result$t.dif == value]. That is:
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[!result$t.dif == value]}, by=ID]
Arun
From: Frank S. <f_j_rod at hotmail.com>
Reply: Frank S. <f_j_rod at hotmail.com>>
Date: September 22, 2014 at 1:16:48 PM
To: datatable-help at lists.r-forge.r-project.org <datatable-help at lists.r-forge.r-project.org>>
Subject: [datatable-help] Two short questions about the operation of data table
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.8
2: 2 1993-07-15 1997-02-25 20.0 3.2
3: 3 1993-05-17 2002-01-01 20.0 20.0
SECOND 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!!
_______________________________________________
datatable-help mailing list
datatable-help at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20140922/3260bad3/attachment-0001.html>
More information about the datatable-help
mailing list