Hii, I am new to R. I wanted to compute the time difference between two date fields, but I am getting the value as 0.
Here is the formula:
alldata$trip_length <- difftime(alldata$end_time,alldata$start_time)
I am getting trip_length as 0
While doing str(alldata), it is showing as-
$ trip_length : 'difftime' num 0 0 0 0 ...
..- attr(*, "units")= chr "secs"
Can anyone help me in this?
Hi, I apologize if I didn't make my data clear earlier. I am working on the Google Certification Program Capstone Project. This is how my sample dataset looks like. I need to calculate the trip length which is ended_at-started_at.
#libraries install
library(tidyverse)
library(tidyr)
library(ggplot2)
library(lubridate)
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
#sample data set
df <- data.frame(
stringsAsFactors = FALSE,
NA,
started_at = c("1/21/2020 20:06",
"1/30/2020 14:22",
"1/9/2020 19:29",
"1/6/2020 16:17",
"1/30/2020 8:37"),
ended_at = c("1/21/2020 20:14",
"1/30/2020 14:26",
"1/9/2020 19:32",
"1/6/2020 16:25",
"1/30/2020 8:42")
)
#There statement is returning 0 secs in trip_length
trip_2021$trip_length <- difftime(trip_2021$ended_at,trip_2021$started_at)
#> Error in as.POSIXct(time1): object 'trip_2021' not found
Created on 2021-09-08 by the reprex package (v2.0.1)
Convert the times to a time format, then use interval:
library(lubridate)
library(tidyverse)
df %>%
mutate(started_at = mdy_hm(started_at),
ended_at = mdy_hm(ended_at)) %>%
mutate(difference = interval(started_at, ended_at) / minutes(1))
# A tibble: 5 x 3
started_at ended_at difference
<dttm> <dttm> <dbl>
1 2020-01-21 20:06:00 2020-01-21 20:14:00 8
2 2020-01-30 14:22:00 2020-01-30 14:26:00 4
3 2020-01-09 19:29:00 2020-01-09 19:32:00 3
4 2020-01-06 16:17:00 2020-01-06 16:25:00 8
5 2020-01-30 08:37:00 2020-01-30 08:42:00 5
As @williaml has pointed out, you need to convert to date time format first. Then you can use interval
, or even difftime
as your attempt would work.
trip_2021 <- data.frame(started_at = c("1/21/2020 20:06", "1/30/2020 14:22", "1/9/2020 19:29", "1/6/2020 16:17", "1/30/2020 8:37"),
ended_at = c("1/21/2020 20:14", "1/30/2020 14:26", "1/9/2020 19:32", "1/6/2020 16:25", "1/30/2020 8:42"))
within(trip_2021,
{
started_at <- as.POSIXct(started_at, format="%m/%d/%Y %H:%M")
ended_at <- as.POSIXct(ended_at, format="%m/%d/%Y %H:%M")
trip_length <- difftime(ended_at, started_at)
})
Hope this helps.
It worked, Thank You so much for your support!
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.