dcast. jazzurro . dcast , . jazzurro, , UTC CRAN 1.10.0 data.table.
1. ifelse
Q,
dcast(
dt, person + door ~ type,
value.var = 'time',
fun.aggregate = function(x) ifelse(type == 'timeIn', min(x), max(x))
)
. fill. , ifelse() POSIXct ( . ?ifelse), .
dcast(
dt, person + door ~ type,
value.var = 'time',
fun.aggregate = function(x)
lubridate::as_datetime(ifelse(type == 'timeIn', min(x), max(x))),
fill = 0
)
# person door timeIn timeOut
#1: ana front door 2016-12-02 07:06:01 2016-12-02 07:06:05
#2: bob front door 2016-12-02 06:05:01 2016-12-02 06:05:05
2. ifelse
ifelse
(tmp <- yes; tmp[!test] <- no[!test]; tmp)
. ,
dcast(
dt, person + door ~ type,
value.var = 'time',
fun.aggregate = function(x) {
test <- type == "timeIn"; tmp <- min(x); tmp[!test] = max(x)[!test]; tmp
}
)
# person door timeIn timeOut
#1: ana front door 2016-12-02 07:06:01 2016-12-02 07:06:05
#2: bob front door 2016-12-02 06:05:01 2016-12-02 06:05:05
, fill, POSIXct .
3. dcast
dcast.data.table fun.aggregate:
dcast(dt, person + door ~ type, value.var = 'time', fun = list(min, max))
# person door time_min_timeIn time_min_timeOut time_max_timeIn time_max_timeOut
#1: ana front door 2016-12-02 07:06:01 2016-12-02 07:06:03 2016-12-02 07:06:02 2016-12-02 07:06:05
#2: bob front door 2016-12-02 06:05:01 2016-12-02 06:05:03 2016-12-02 06:05:02 2016-12-02 06:05:05
dcast(dt, person + door ~ type, value.var = 'time', fun = list(min, max))[
, .(person, door, timeIn = time_min_timeIn, timeOut = time_max_timeOut)]
# person door timeIn timeOut
#1: ana front door 2016-12-02 07:06:01 2016-12-02 07:06:05
#2: bob front door 2016-12-02 06:05:01 2016-12-02 06:05:05
, jazzurro
dt <- structure(list(person = c("bob", "bob", "bob", "bob", "ana",
"ana", "ana", "ana"), door = c("front door", "front door", "front door",
"front door", "front door", "front door", "front door", "front door"
), type = c("timeIn", "timeIn", "timeOut", "timeOut", "timeIn",
"timeIn", "timeOut", "timeOut"), time = structure(c(1480658701,
1480658702, 1480658703, 1480658705, 1480662361, 1480662362, 1480662363,
1480662365), class = c("POSIXct", "POSIXt"))), .Names = c("person",
"door", "type", "time"), row.names = c(NA, -8L), class = c("data.table",
"data.frame"))
UTC.
dt[, time := lubridate::with_tz(time, "UTC")]
dt
# person door type time
#1: bob front door timeIn 2016-12-02 06:05:01
#2: bob front door timeIn 2016-12-02 06:05:02
#3: bob front door timeOut 2016-12-02 06:05:03
#4: bob front door timeOut 2016-12-02 06:05:05
#5: ana front door timeIn 2016-12-02 07:06:01
#6: ana front door timeIn 2016-12-02 07:06:02
#7: ana front door timeOut 2016-12-02 07:06:03
#8: ana front door timeOut 2016-12-02 07:06:05
.