Results is 0 in difftime

Im currently working with a dataframe from a Cyclist company, im trying my best to get a repex working but i dont know how to upload my data.

Im trying to get the use time of the service but the only outcome that i have from this function is 0 and in some 144s

datapasta::df_paste(head(data, 5)[, c('started_at', 'ended_at','use_time')])


data.frame(
  stringsAsFactors = FALSE,
                                     started_at = c("2020-04-26 17:45:14",
                                                    "2020-04-17 17:08:54",
                                                    "2020-04-01 17:54:13",
                                                    "2020-04-07 12:50:19","2020-04-18 10:22:59"),
                                       ended_at = c("2020-04-26 18:12:03",
                                                    "2020-04-17 17:17:03",
                                                    "2020-04-01 18:08:36",
                                                    "2020-04-07 13:02:31","2020-04-18 11:15:54"),
          use_time = c("0", "0", "0", "0", "0")
          )
#>            started_at            ended_at use_time
#> 1 2020-04-26 17:45:14 2020-04-26 18:12:03        0
#> 2 2020-04-17 17:08:54 2020-04-17 17:17:03        0
#> 3 2020-04-01 17:54:13 2020-04-01 18:08:36        0
#> 4 2020-04-07 12:50:19 2020-04-07 13:02:31        0
#> 5 2020-04-18 10:22:59 2020-04-18 11:15:54        0


data$use_time <- difftime( as.Date(data$ended_at)   ,
                                   as.Date(data$started_at) ,
                                   units = "mins")

Created on 2022-01-27 by the reprex package (v2.0.1)

Hi, can you provide a reproducible example of some of your dataset?

Hi as i said in my post i really dont know how to upload my data, but the example that i have seems optimal

started_at Ended_at use_time

12/18/2019 4:06:59 PM 12/18/2019 4:07:05 PM 0mins
12/18/2019 4:07:26 PM 12/18/2019 4:07:28 PM 0mins
12/17/2019 6:48:06 PM 12/17/2019 6:48:07 PM 0mins
12/17/2019 6:25:16 PM 12/17/2019 6:25:22 PM 0mins

Hi, have a look at the article. It tells you how to upload data. It will mean that people are more likely to try and answer your question too. What you have posted is not reproducible.

Hi, im trying to get the use time of a service that has a stamp time but the result that i get with timediff() is 0
i have tried to use as.POSIXct, as.Date but the result is the same

The data that im working its a combination of the csv's of 12 months so its 19 x 3.6m rows

datapasta::df_paste(head(Cyclist_data, 5)[, c('started_at', 'ended_at','use_time')])
#> Error in head(Cyclist_data, 5): objeto 'Cyclist_data' no encontrado

data.frame(
  stringsAsFactors = FALSE,
                                     started_at = c("2020-04-26 17:45:14",
                                                    "2020-04-17 17:08:54",
                                                    "2020-04-01 17:54:13",
                                                    "2020-04-07 12:50:19","2020-04-18 10:22:59"),
                                       ended_at = c("2020-04-26 18:12:03",
                                                    "2020-04-17 17:17:03",
                                                    "2020-04-01 18:08:36",
                                                    "2020-04-07 13:02:31","2020-04-18 11:15:54"),
          use_time = c("0", "0", "0", "0", "0")
          )
#>            started_at            ended_at use_time
#> 1 2020-04-26 17:45:14 2020-04-26 18:12:03        0
#> 2 2020-04-17 17:08:54 2020-04-17 17:17:03        0
#> 3 2020-04-01 17:54:13 2020-04-01 18:08:36        0
#> 4 2020-04-07 12:50:19 2020-04-07 13:02:31        0
#> 5 2020-04-18 10:22:59 2020-04-18 11:15:54        0


Cyclist_help$use_time <- difftime( as.Date(Cyclist_data$ended_at)   ,
                                   as.Date(Cyclist_data$started_at) ,
                                   units = "mins")
#> Error in as.Date(Cyclist_data$ended_at): objeto 'Cyclist_data' no encontrado

Created on 2022-01-27 by the reprex package (v2.0.1)

1 Like

The data for the date times start as characters in this example, so I convert them to POSIXct.

Cyclist_data <- data.frame(
  stringsAsFactors = FALSE,
  started_at = c("2020-04-26 17:45:14",
                 "2020-04-17 17:08:54",
                 "2020-04-01 17:54:13",
                 "2020-04-07 12:50:19","2020-04-18 10:22:59"),
  ended_at = c("2020-04-26 18:12:03",
               "2020-04-17 17:17:03",
               "2020-04-01 18:08:36",
               "2020-04-07 13:02:31","2020-04-18 11:15:54"),
  use_time = c("0", "0", "0", "0", "0")
)
Cyclist_data
#>            started_at            ended_at use_time
#> 1 2020-04-26 17:45:14 2020-04-26 18:12:03        0
#> 2 2020-04-17 17:08:54 2020-04-17 17:17:03        0
#> 3 2020-04-01 17:54:13 2020-04-01 18:08:36        0
#> 4 2020-04-07 12:50:19 2020-04-07 13:02:31        0
#> 5 2020-04-18 10:22:59 2020-04-18 11:15:54        0
str(Cyclist_data)
#> 'data.frame':    5 obs. of  3 variables:
#>  $ started_at: chr  "2020-04-26 17:45:14" "2020-04-17 17:08:54" "2020-04-01 17:54:13" "2020-04-07 12:50:19" ...
#>  $ ended_at  : chr  "2020-04-26 18:12:03" "2020-04-17 17:17:03" "2020-04-01 18:08:36" "2020-04-07 13:02:31" ...
#>  $ use_time  : chr  "0" "0" "0" "0" ...
Cyclist_data$started_at <- as.POSIXct(Cyclist_data$started_at)
Cyclist_data$ended_at <- as.POSIXct(Cyclist_data$ended_at)
str(Cyclist_data)
#> 'data.frame':    5 obs. of  3 variables:
#>  $ started_at: POSIXct, format: "2020-04-26 17:45:14" "2020-04-17 17:08:54" ...
#>  $ ended_at  : POSIXct, format: "2020-04-26 18:12:03" "2020-04-17 17:17:03" ...
#>  $ use_time  : chr  "0" "0" "0" "0" ...
Cyclist_data$use_time <- difftime( Cyclist_data$ended_at,
                                   Cyclist_data$started_at,
                                   units = "mins")
Cyclist_data
#>            started_at            ended_at      use_time
#> 1 2020-04-26 17:45:14 2020-04-26 18:12:03 26.81667 mins
#> 2 2020-04-17 17:08:54 2020-04-17 17:17:03  8.15000 mins
#> 3 2020-04-01 17:54:13 2020-04-01 18:08:36 14.38333 mins
#> 4 2020-04-07 12:50:19 2020-04-07 13:02:31 12.20000 mins
#> 5 2020-04-18 10:22:59 2020-04-18 11:15:54 52.91667 mins

Created on 2022-01-28 by the reprex package (v2.0.1)

3 Likes

Similar to @FJCC

library(lubridate)
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
bikes <- data.frame(
  started_at = c("2020-04-26 17:45:14",
                 "2020-04-17 17:08:54",
                 "2020-04-01 17:54:13",
                 "2020-04-07 12:50:19","2020-04-18 10:22:59"),
  ended_at = c("2020-04-26 18:12:03",
               "2020-04-17 17:17:03",
               "2020-04-01 18:08:36",
               "2020-04-07 13:02:31","2020-04-18 11:15:54"),
  use_time = c("0", "0", "0", "0", "0")
)

bikes$use_time <- 0
bikes$started_at <- ymd_hms(bikes[,1])
bikes$ended_at <- ymd_hms(bikes[,2])

bikes$use_time <- bikes[,2] - bikes[,1]

bikes
#>            started_at            ended_at      use_time
#> 1 2020-04-26 17:45:14 2020-04-26 18:12:03 26.81667 mins
#> 2 2020-04-17 17:08:54 2020-04-17 17:17:03  8.15000 mins
#> 3 2020-04-01 17:54:13 2020-04-01 18:08:36 14.38333 mins
#> 4 2020-04-07 12:50:19 2020-04-07 13:02:31 12.20000 mins
#> 5 2020-04-18 10:22:59 2020-04-18 11:15:54 52.91667 mins
2 Likes

Everytime y try this solution i always get the message

"Error in as.POSIXlt.character(x, tz, ...) :
character string is not in a standard unambiguous format"

You need to tell as.POSIXct() what the format of your data is unless it is year-month-day or year/month/day. What is the format of your data?

The format of the data that i have is year/month/day hour/minute/second

I do not get an error using a date-time with the format I think you have but the time element is ignored. If I define the format, I get the correct result.

as.POSIXct("2022/02/01 13/12/45") #time is ignored in the result
[1] "2022-02-01 MST"
as.POSIXct("2022/02/01 13/12/45", format = "%Y/%m/%d %H/%M/%S")
[1] "2022-02-01 13:12:45 MST"

This worked!, thanks you so much.

I found this error so weird because on the R studio on my machine it doesnt work, but in kaggle it works fine, maybe its something with the format

Thanks you so much i learned something new thanks to you

This topic was automatically closed 7 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.