I have some dates and times in a .csv I'm bringing into R.
I then set them as a POSIXct
object and specify the format. Making this change changes the format of how the date-time is displayed. I can then force it back to the original format by using the format function but then the object becomes a character and is no longer POSIXct. Is there a way to tell POSIXct what format for the date I want or to set a global default for how dates and date-times are displayed in R? I'd love to be able to just set my date-time once and have it be how I want it to be.
Here's some sample code:
# some data
df = data.frame(other_var = c("something", "else", "here"),
date_time = c("6/5/2020 00:10:10", "6/6/2020 00:10:11", "6/7/2020 01:10:11"))
# set as.POSIXct
df$date_time = as.POSIXct(df$date_time, format = "%m/%d/%Y %H:%M:%S", tz="UTC")
# check structure
str(df$date_time)
# make pretty
df$date_time = format(df$date_time, format = "%m/%d/%Y %H:%M:%S", tz="UTC")
# check structure
str(df$date_time) #is character! :(