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. <[email protected]>
Reply: Frank S. <[email protected]>>
Date: September 22, 2014 at 1:16:48 PM
To: [email protected]
<[email protected]>>
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
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
_______________________________________________
datatable-help mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help