Converting character string to local time [duplicate] - r

This question already has answers here:
How can I fix corrupted dates in R?
(1 answer)
R datetime series missing values
(3 answers)
Closed 7 months ago.
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?

Related

Reformatting date and timestamp with r [duplicate]

This question already has answers here:
Changing date format in R
(7 answers)
Closed 3 years ago.
In order to prevent an error in uploading xls data into a sql database, I am trying to reformat a date type of "08/22/2019 02:05 PM CDT" and want only the date, not the time or the timezone. Many efforts to use the default, POSIX and lubridate actions have failed. The xls file formats the date column as general.
I have a column of data to convert, not a single cell. This is a part of a loop for multiple files in a folder.
Failures:
#mydata_r11_Date2 <- strptime(as.character(mydata_r11_Date$Date), "%d/%m/%Y")
# parse_date_time(x = mydata_r11_Date$Date,
# orders = c("d m y", "d B Y", "m/d/y"),
# locale = "eng")
#
#
# mydata_r11_Date <- as.character(mydata_r11_Date)
mydata_r11_Date <- gsub('^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]+[+-][0-9]{2}):([0-9]{2})$',
'\\1\\2',
mydata_r11_Date$Date)
ymd_hms(mydata_r11_Date$Date)
mydata_r11_Date <- as_date (mydata_r11_Date$Date,format = "%Y-%m-%d")
mydata_r11_Date2 <- format(as.Date(mydata_r11_Date,"%Y-%m-%d"),"%Y-%m-%d")
Errors include:
Warning message:
All formats failed to parse. No formats found.
Error in as.Date.default(x, ...) :
do not know how to convert 'x' to class “Date”
Error in as.Date.default(mydata_r11_Date$Date, format = "%Y-%m-%d") :
do not know how to convert 'mydata_r11_Date$Date' to class “Date”
Error: unexpected ',' in " mydata_r11_Date <- as.Date(mydata_r11_Date$Date),"
Error in as_date(x) : object 'x' not found
library(readxl)library(reshape2) library(lubridate)
import xsl
mydata_r11 <- read_excel("C:/FOLDER/FOLDER/FOLDER/OUTPUT/WADUJONOKO_student_assessment_results.xls",1,skip = 1, col_types = "list")
Isolate date column
mydata_r11_Date <- mydata_r11[,c(8)]
Convert date
mydata_r11_Date 2 <-
Have "08/22/2019 02:05 PM CDT"
Want "08/22/2019"
I don't understand why you are resorting to complex regex here when you seem to only want the date component, which is the first 10 characters of the timestamps. Just take the substring and then call as.Date with an appropriate format mask:
x <- "08/22/2019 02:05 PM CDT"
y <- substr(x, 1, 10)
as.Date(y, format = "%m/%d/%Y")
[1] "2019-08-22"

String format parsing in R [duplicate]

This question already has answers here:
How to convert dd/mm/yy to yyyy-mm-dd in R
(6 answers)
Closed 3 years ago.
When I parse a character string into a date, Why does this not throw an error or an NA? I have tried the following
t <- "31-Oct-2012"
as.Date(t, format = "%d-%B-%Y") # this produces the expected result
as.Date(t, format = "%d-%B-%y") # I was expecting an NA
Instead I get
[1] "2020-10-31"
Because %y is for two digit year, so it takes only first two digits and ignores the rest. It treats t as
as.Date("31-Oct-20", format = "%d-%B-%y")
#[1] "2020-10-31"
This also works when you have anything after 2-digit year. See
as.Date("31-Oct-20ABC", format = "%d-%B-%y")
#[1] "2020-10-31"
R tries to "auto-complete" when there is less information, it returns some (incorrect) date for
as.Date("31-Oct-20", format = "%d-%B-%Y")
#[1] "0020-10-31"
but returns NA for
as.Date("31-Oct-ABC20", format = "%d-%B-%y")
#[1] NA

How to Split Time and Date in a Column in R [duplicate]

This question already has answers here:
Split date-time column into Date and time variables
(7 answers)
Closed 4 years ago.
Date Assigned
<dttm>
2017-09-02 14:25:00
I am wondering how to separate the date from the time, into two separate columns, for easier plotting.
I have tried the following code, but it has returned NA;
Hours <- format(as.POSIXct(strptime(INV$`Date Assigned`, "%Y/%m/%d %H:%M", tz ="")),
format = "H%:%M")
Dates <- format(as.POSIXct(strptime(INV$`Date Assigned`,"%Y/%m/%d",tz="")),
format = "%d/%d/%Y")
Any suggestions?
Try:
Hours <- format(as.POSIXct(INV$`Date Assigned`, "%Y-%m-%d %H:%M:%S", tz = ""), format = "%H:%M")
Dates <- format(as.Date(INV$`Date Assigned`,"%Y-%m-%d"), format = "%d/%m/%Y")
Output:
Hours
[1] "14:25"
Dates
[1] "02/09/2017"
Of course if you want them as columns in your dataframe, you need to assign them as such:
INV$Hours <- format(as.POSIXct(INV$`Date Assigned`, "%Y-%m-%d %H:%M:%S", tz = ""), format = "%H:%M")
INV$Dates <- format(as.Date(INV$`Date Assigned`,"%Y-%m-%d"), format = "%d/%m/%Y")
Output:
Date Assigned Hours Dates
1 2017-09-02 14:25:00 14:25 02/09/2017

convert orderdate of format m/d/yy to YYYY-MM-DD in R

My orderdate is in factor and i want to convert it into from mm/dd/yy format to YYYY-MM-DD format.
orderdate : Factor w/ 155932 levels "1/1/2017 1:05:00 AM",..: 41 1 1 89 100 102 106 107 119 122 ...
I tried couple of things:
orders2017$newdate <- (factor(orders2017$orderdate))
orders2017$newdate1 <- as.Date(orders2017$newdate,format="%Y-%m-%d")
but nothing is working out and giving me new columns as empty. Any help is appreciated
If you really have values like "1/1/2017 1:05:00 AM" then those aren't dates, they are date times, and as such you have to specify formatting characters for both the date and time parts.
So, first you need to get your date times into a form R understands as such (e.g. POSIXct) by specifying all the parts of the date time:
test <- as.POSIXct("1/1/2017 1:05:00 AM", format = '%m/%d/%Y %I:%M:%S %p')
test
> test
[1] "2017-01-01 01:05:00 CST"
See ?strftime if you are not familiar with all the formatting placeholders used above, and note the conditions for use of %I and %p.
Then you can convert the POSIXct vector into the date format you desire
format(test, format = '%Y-%m-%d')
> format(test, format = '%Y-%m-%d')
[1] "2017-01-01"
A complication for you is that R has converted your character date times into a factor, so you need to convert them back to a character vector before converting to date times. For example (not tested as you didn't supply example data)
orders2017 <- transform(orders2017,
orderdate = as.POSIXct(as.character(orderdate),
format = '%m/%d/%Y %I:%M:%S %p'))
orders2017 <- transform(orders2017,
newdate = format(orderdate, format = '%Y-%m-%d'))
You were really close with as.Date(orders2017$newdate,format="%Y-%m-%d"), you just need to make the format string match your actual format.
Your actual format is mm/dd/YYYY, so use %m/%d/%Y as the format string:
as.Date("1/1/2017 1:05:00 AM", format = "%m/%d/%Y")
# [1] "2017-01-01"
Then the default printing of Date format objects is what you want.
So for your data,
orders2017$newdate1 <- as.Date(orders2017$newdate,format="%Y/%m/%d")
The time part will just be ignored.

How to extract the hour of this kind of timestamp? [duplicate]

This question already has an answer here:
Extracting 2 digit hour from POSIXct in R
(1 answer)
Closed 7 years ago.
This is how my timestamp looks like
date1 = timestamp[1]
date1
[1] 07.01.2016 11:52:35
3455 Levels: 01.02.2016 00:11:52 01.02.2016 00:23:35 01.000:30:21 31.01.2016 23:16:18
When I try to extract the hour, I get an invalid 'trim' argument. Thank you !
format(date1, "%I")
Error in format.default(structure(as.character(x), names = names(x), dim = dim(x), :
invalid 'trim' argument
How can I extract single components like the hour from this timestamp.
With base R:
format(strptime(x,format = '%d.%m.%Y %H:%M:%S'), "%H")
[1] "11"
data
x <- as.factor("07.01.2016 11:52:35")
You first need to parse the time
d = strptime(date1,format = '%d.%m.%Y %H:%M:%S')
Then use lubridate to extract parts like hour etc.
library(lubridate)
hour(d)
minute(d)

Resources