0

I'm appending two datasets where both have columns for Date (YYYY/MM/DD HH:MM:SS), but one is a character and the other is date. The first df lists Date as "2022-01-01 01:00:00" and the second (crimes) lists Date as 01/01/2022 12:00:00 AM.

For my purposes, I do not need the HHMMSS or timezone characteristics.

I've tried:

crimes$Date <- format(as.POSIXct(crimes$Date,format='%I:%M %p'),format="%H:%M:%S")

crimes$Date <- format(as.POSIXct(crimes$Date,format="%Y-%m-%d %I:%M:%S %p"))

crimes$Date <- as.numeric(as.character(crimes$Date)) 

crimes$Date <- strptime(crimes$Date, format = "%Y-%m-%d %H:%M:%OS", tz = "EST")

Each of these changes the observations under date as NA.

Other options I've tried (and their error messages):

crimes$Date <- parse_date_time("%Y/%m/%d %I:%M:%S %p")

#Error in .best_formats(train, orders, locale = locale, select_formats,  : 
                         #argument "orders" is missing, with no default
crimes$Date  <- format(as.POSIXct(crimes$Date), format='%y-%m-%d')

#Error in as.POSIXlt.character(x, tz, ...) : 
#character string is not in a standard unambiguous format

What am I missing?

  • Try https://stackoverflow.com/a/70304571/3358272, it supports multiple formats and combines them into one vector. Once you have this, you can easily discard (if desired) the time component. (You could also adapt it to an `as.Date` variant after removing all time parts from the strings, if you need to. See https://stackoverflow.com/a/52319606/3358272.) – r2evans Jul 15 '22 at 16:14
  • If neither of those links works for you, then please [edit] your question to add sample data (see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info), then @ping me and we can resolve what is different. – r2evans Jul 15 '22 at 16:16

0 Answers0