Trying to split date/time

I am new to rstudio and running trying to extract the date only from a date time column.
The date/time is formatted like this:
4/12/2016 12:00:00 AM

I tried this script:

extracting date

data_frame$date <- as.Date (data_frame$datetime)
print ("Modified DataFrame")
print (data_frame)

That worked except that for a number of rows I get an N/A value in the date column where I can clearly see there is a time in the original.
Suggestions?

Hi, welcome to the forum.

We need a reproducible example (reprex)

Your date values don't have an unambiguous standard format, that is why as.Date() fails. It is way easier to use the lubridate package to deal with this, see this example.

library(tidyverse)
library(lubridate)

# Sample data in a copy/paste friendly format
data_frame <- data.frame(
    datetime = c("4/12/2016 12:00:00 AM", "4/12/2016 13:00:00 AM")
)

# Relevant code
data_frame %>% 
    mutate(datetime = dmy_hms(datetime),
           date = as.Date(datetime))
#>              datetime       date
#> 1 2016-12-04 00:00:00 2016-12-04
#> 2 2016-12-04 13:00:00 2016-12-04

Created on 2022-06-14 by the reprex package (v2.0.1)

In the first part of the code where you standardize the format, wow would I apply the datetime changes to the entire column? (forgive me - I am a newbie)

Sorry but I don't understand what you mean, all the commands are applied to entire columns, not individual values. Can you elaborate on your question?

Just in case you are confused about the sample data I used, that is just for exemplification, you are supposed to simply use your own data frame instead.

Sorry I am still lost. Can I apply the changes to the existing data frame or should I create a new one for this column?
Somehow I can do part of what you recommend but the changes only apply to the values I enter in
c(" ", " ")
and not the entire column (there are 413 rows). What am I missing? I feel like I know what I want to do, but I am in way over my head and I just can't figure it out.

You are not supposed to do that part, as I said, that is just sample data for exemplification. The part that directly applies to your problem is labeled as "#Relevant code" and you can simply use your existing data frame instead. To better understand why I include sample data in my answer, read the reprex guide in the link @jrkrideau gave you before.

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.