-2

Im new to coding in R and cant seem to figure out what the problem here is, the difftime() function always shows the results as 0 when I try to make a new column.

all_trips$ride_length <- difftime(all_trips$ended_at,all_trips$started_at)

Glimpse

$ started_at         <chr> "2019-04-01 00:02:22", 
$ ended_at           <chr> "2019-04-01 00:09:48",
user438383
  • 5,716
  • 8
  • 28
  • 43

1 Answers1

2

Welcome to SO! In general please post a minimal reproducible example of the relevant parts of your dataset using dput() or similar.

The <chr> in your output means that your columns are a character vector rather than date time.

As pointed out in the comments by jay.sf, difftime() can parse character vectors in some formats, and it should be able to parse the single example given in your post.

Nevertheless, in general it is better to store datetimes as one of the built-in datetime classes, i.e. either POSIXct or POSIXlt, rather than to rely on implicit coercion. To convert them:

all_trips$started_at  <- as.POSIXct(all_trips$started_at)
all_trips$ended_at  <- as.POSIXct(all_trips$ended_at)

# Calculate diff time
all_trips$ride_length <- difftime(
    all_trips$ended_at,
    all_trips$started_at,
    units = "secs"
)

You will get an error if the conversion does not work:

as.POSIXct(c("2019-04-01 00:02:22", "not a valid date time"))
Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

This is the same error you will get if you try to use difftime() with a character vector that is not unambiguously a datetime.

This post is a useful discussion of R's internal handling of date/time classes.

SamR
  • 8,826
  • 3
  • 11
  • 33
  • I have done that but it still gives me the results as 0, and I see that it has converted the date time into date (if i am not mistaken). I need the results to be in second, probably should have specified it earlier – Sergej Trifunovic Jul 12 '22 at 12:41
  • You need to add the `units = secs` argument - I have updated the post. If this still does not work - what is the output of `sapply(all_trips, class)`? – SamR Jul 12 '22 at 12:45
  • Here is the `sapply` ` $ride_id [1] "character" $started_at [1] "POSIXct" "POSIXt" $ended_at [1] "POSIXct" "POSIXt" $rideable_type [1] "character" $start_station_id [1] "integer" $start_station_name [1] "character" $end_station_id [1] "integer" $end_station_name [1] "character" $member_casual [1] "character" $date [1] "Date" $month [1] "character" $day [1] "character" $year [1] "character" $day_of_week [1] "character" $ride_length [1] "difftime" ` – Sergej Trifunovic Jul 12 '22 at 13:44
  • I tried it with ` units = secs` but still gives out the same result 0, if it gives any insight, before I converted it from `` to `POSIXct` the processing time was like 2 mins, now it does it instantly. – Sergej Trifunovic Jul 12 '22 at 13:45
  • The code should work with the one example you have posted so it is hard to see what the problem is. I think you need to post a more representative subset of your data, using `dput()`. – SamR Jul 12 '22 at 13:51