Unable to convert a date formatted as chr to date; appears as datetime

I am new to R and Rstudio. I am currently trying to convert a column of dates that appear as mm/dd/yyyy hh:mm:ss but are classed as character. I want to turn them into date format, so that the hh:mm:ss is dropped off in a new column.
I have tried finding a solution online in guides but all the guides I have found already have the date as mm/dd/yyyy, which is not my starting point.
Here is an example of the data frame I am working with:

df <- data.frame(date=c('12/31/2016 23:57:52', '2022-02-18 22:55:45'),
                 sales=c(14, 29))

I have tried using separate() to make 2 columns, one for date and the other for time, but it brings back an error for an 'unexpected symbol'. here is the line of code that brings back that error:

separate(df, date, into=c('date_1', 'time') sep=' ')

Please help me out if you have the time, I feel like the solution is something simple I just can't figure out what.

You forgot a comma

separate(df, date, into=c('date_1', 'time'), sep=' ')
2 Likes

Thanks! I was trying to be careful about syntax but I see I wasn't. It worked just fine after that.

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.