I need to calculate the timed difference and make a variable 'duration'. The sample data looks like this:
endTime startTime user_id categories duration
1 2019-02-22T19:17:02.618 019-02-22T19:16:58.377 10224 communication 0 secs
2 2019-02-23T12:19:01.055 2019-02-23T12:18:44.414 10224 communication 0 secs
3 2019-02-25T21:03:15.771 2019-02-25T21:03:06.961 10224 utility & tools 0 secs
4 2019-02-27T19:22:41.174 2019-02-27T19:22:32.246 10224 communication 0 secs
endTime and startTime are all set to Date format using as.POSIXct. I use the difftime in base R, code looks like this:
dat$duration <- difftime(dat$startTime,dat$endTime)
and all the duration has a value of 0. I don't understand why this is happening.
I checked some other libraries too (chron
, lubridate
) for calculating this, seems that they only accept one string containing both times, instead of two variables. Doesn't seem like wise for me to merge both variables into one string..is there an easier way? Thank you!!
dput:
structure(list(battery = c(47L, 41L, 18L, 94L, 94L, 93L, 73L,
73L, 47L, 49L), endTime = c("2019-02-22T19:17:02.618", "2019-02-23T12:19:01.055",
"2019-02-25T21:03:15.771", "2019-02-27T19:22:41.174", "2019-02-27T19:22:53.256",
"2019-02-27T23:51:16.407", "2019-03-02T20:18:28.090", "2019-03-02T20:18:43.488",
"2019-03-19T13:07:16.993", "2019-03-19T12:16:36.962"), session = c(1550859371L,
1550920714L, 1551124876L, 1551291720L, 1551291720L, 1551307871L,
1551554295L, 1551554295L, 1552997232L, 1552994133L), startTime = c("2019-02-22T19:16:58.377",
"2019-02-23T12:18:44.414", "2019-02-25T21:03:06.961", "2019-02-27T19:22:32.246",
"2019-02-27T19:22:45.404", "2019-02-27T23:51:15.270", "2019-03-02T20:18:21.362",
"2019-03-02T20:18:37.066", "2019-03-19T13:07:15.348", "2019-03-19T12:15:38.440"
), user_id = c(10224L, 10224L, 10224L, 10224L, 10224L, 10224L,
10224L, 10224L, 10224L, 10224L), categories = structure(c(1L,
1L, 6L, 1L, 2L, 2L, 6L, 1L, 2L, 1L), .Label = c("communication",
"games & entertainment", "lifestyle", "news & information outlet",
"social network", "utility & tools"), class = "factor"), duration = structure(c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0), class = "difftime", units = "secs")), row.names = c(NA,
10L), class = "data.frame")