Trouble converting character to date

Hello, I am new to R but have done my best to get rid of any silly mistakes and look for answers on my own. So far, no luck. I am trying to change a column of dates(in chr format, seen as mm/dd/yyyy) to dates(in date format) in order to create 4 new columns each for %Y, %m, %d, and %A. I am able to get this code chunk to run:

q4_2016$date <- as.Date(q4_2016$start_date, "%Y/%m/%d") #The default format is yyyy-mm-dd
q4_2016$month <- format(as.Date(c(q4_2016$date), "%m"))
q4_2016$day <- format(as.Date(q4_2016$date), "%d")
q4_2016$year <- format(as.Date(q4_2016$date), "%Y")
q4_2016$day_of_week <- format(as.Date(q4_2016$date), "%A")

But for some reason, after running it, glimpse reveals no permanent changes. I tried piping in the updated code chunk to make it permanent, but it got rid of my entire dataset, so I reloaded my last save. Here is what my glimpse looks like after using the above code chunk:

> glimpse(q4_2016)
Rows: 683,832
Columns: 19
$ ride_id            <chr> "12979228", "12979227", "12979226", "12979225", "12…
$ started_at         <chr> "12/31/2016 23:57:52", "12/31/2016 23:53:18", "12/3…
$ ended_at           <chr> "1/1/2017 00:06:44", "1/1/2017 00:08:13", "1/1/2017…
$ rideable_type      <chr> "5076", "5114", "1026", "504", "4451", "5643", "48"…
$ tripduration       <dbl> 532, 895, 931, 970, 980, 179, 1863, 1867, 1656, 108…
$ start_staion_id    <dbl> 502, 195, 195, 199, 199, 47, 177, 177, 195, 264, 15…
$ start_station_name <chr> "California Ave & Altgeld St", "Columbus Dr & Rando…
$ end_station_id     <dbl> 258, 25, 25, 35, 35, 125, 140, 140, 195, 52, 42, 77…
$ end_station_name   <chr> "Logan Blvd & Elston Ave", "Michigan Ave & Pearson …
$ member_casual      <chr> "casual", "casual", "casual", "member", "member", "…
$ date               <date> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ month              <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ day                <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ year               <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ day_of_week        <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
$ start_date         <chr> "12/31/2016", "12/31/2016", "12/31/2016", "12/31/20…
$ start_time         <chr> "23:57:52", "23:53:18", "23:53:07", "23:51:31", "23…
$ end_date           <chr> "1/1/2017", "1/1/2017", "1/1/2017", "1/1/2017", "1/…
$ end_time           <chr> "00:06:44", "00:08:13", "00:08:38", "00:07:41", "00…

Which is the same glimpse from before using the code chunk. The 'started_at' and 'ended_at' columns are teh source of the 'start_date', 'start_time', 'end_date', and 'end_time' columns. If anyone can help me figure out what I have done wrong, it would be greatly appreciated. Thanks!

You need to change the format for the date in the first line:

q4_2016$date <- as.Date(q4_2016$start_date, "%m/%d/%Y")

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.