converting column types

I am a new R learner and I am working at the Cyclistic Bike case study. In the dataset there is the column "started_at" which is in the "MM/DD/YYYY HH:MM" format. The column is in the character format and I am trying to extract the date info into separate year, month, day columns. The following code chunk is what I am trying to run:
jan22 <- jan22 %>%
mutate(year = format(as.Date(started_at), "%Y")) %>%
mutate(month = format(as.Date(started_at), "%M")) %>%
mutate(date = format(as.Date(started_at), "%d")) %>%
mutate(day_of_week = format(as.Date(started_at), "%A")) %>%
mutate(ride_length = difftime(ended_at, started_at)) %>%
mutate(start_time = strftime(started_at, "%H"))

When I run the code I receive the following error:
Error in mutate():
! Problem while computing year = format(as.Date(started_at), "%Y").
Caused by error in charToDate():
! character string is not in a standard unambiguous format
Run rlang::last_error() to see where the error occurred.

I am hoping someone can help me figure this out. Thus far I have not understood the Google solutions.

You have to tell the as.Date() function what the format of the input date is. Here is an example:

format(as.Date("01/11/2023 03:42", format = "%m/%d/%Y %H:%M"), "%d")
[1] "11"

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.