0

I am doing a case study on bike share. Trying to calculate the time spent (in minutes) by each user , which would occur by subtracting the end time by start time.

enter image description here

I first tried to create new start_time and end_time columns .After which I can do calculations, using the following commands:

df_cyclistic_copy$started_at_time <- as.POSIXct(df_cyclistic_copy$started_at_datetime, format = "%d-%m-%Y %H:%M")
df_cyclistic_copy$ended_at_time <- as.POSIXct(df_cyclistic_copy$ended_at, format = "%d-%m-%Y %H:%M")

A column is created but with NA

enter image description here

I even checked the class of the new started_at_time column. It is POSIXct

enter image description here

I do not understand. Where am I wrong? (Note: I am new to programming in general)

  • Your columns are already in the correct format. No need for `format` there – akrun Oct 27 '22 at 15:54
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Please [do not post code or data in images](https://meta.stackoverflow.com/q/285551/2372064) – MrFlick Oct 27 '22 at 17:38

1 Answers1

0

The columns are in the default format for Datetime, i.e . ymd hms. We just need to use as.POSIXct. The format if need to be specified, it would be "%Y-%m-%d %H:%M" instead of "%d-%m-%Y %H:%M" as the string starts with 4 digit year (%Y) followed by - then 2 digit month (%m), - , 2 digit day (%d)

df_cyclistic_copy$started_at_time <- as.POSIXct(df_cyclistic_copy$started_at_datetime)
df_cyclistic_copy$ended_at_time <- as.POSIXct(df_cyclistic_copy$ended_at)

as an example

> as.POSIXct("2022-04-06 8:52")
[1] "2022-04-06 08:52:00 EDT"
akrun
  • 874,273
  • 37
  • 540
  • 662