How can as.Date() convert fully written dates into ISO 8601? [duplicate] - r

This question already has an answer here:
Format for ordinal dates (day of month with suffixes -st, -nd, -rd, -th)
(1 answer)
Closed 1 year ago.
I currently have a vector of dates that are in the following format:
a <- c("Wednesday 26th May 2021","Thursday 27th May 2021")
I've tried to get it into ISO 8601 using the following:
as.Date(a, "%I %d%S %F %Y")
But I'm not 100% certain about the syntax of writing dates.
Any thoughts are appreciated!

You can remove the date suffixes and use as.Date -
#Added an extra date that does not have th as prefix.
a <- c("Wednesday 26th May 2021","Thursday 27th May 2021",
'Tuesday 1st June 2021', 'Monday 31st May 2021')
as.Date(sub('(?<=\\d)(th|rd|st|nd)', '', a, perl = TRUE), '%A %d %b %Y')
#[1] "2021-05-26" "2021-05-27" "2021-06-01" "2021-05-31"
Read ?strptime for different format specification.
If you are open to packages lubridate::dmy works directly.
lubridate::dmy(a)
#[1] "2021-05-26" "2021-05-27" "2021-06-01" "2021-05-31"

Related

Date String conversion in R via as.Date() results in NAs [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 12 months ago.
This question has appeared before, I know, but I haven't been able to derive the correct output after running Sys.setlocale("LC_TIME", "american").
Ok, so I have dataframe with a column of dates that look something like "October 2020" and "June 2021", etc.
I run as.Date(dataframe$month_column, format = "%B %Y"), and I do this after running Sys.setlocale() with the above inputs. But my output continues to be a bunch of NAs.
I suppose one work around I could try would be to split the Month and Year parts by the space into two columns, which might make it easier to coerce into date types, but I'd like to avoid that if possible, since I want to plot quantities by Month Year.
Any insight would be appreciated.
You can convert character strings of month and years with the my() function in the lubridate package. my, in this case, stands for month-year (there are also other functions, like mdy() which look for entries in the form of month-day-year, and dym() which expect day-month-year order).
library(lubridate)
dates <- data.frame(
dt = c("October 2020", "June 2021")
)
dates$converted <-my(dates$dt)
dates
Output:
dt converted
1 October 2020 2020-10-01
2 June 2021 2021-06-01

Can format like "May 17, 2017", "17/5/2017" or "17-5-17 05:24:39" be used in as.POSIXlt?

I've just read about the difference between POSIXlt and POSIXct and it is said that POSIXlt is a mixed text and character format like "May, 6 1985", "1990-9-1" or "1/20/2012". When I try such kind of things I get an error
as.POSIXlt("May, 6 1985")
# character string is not in a standard unambiguous format
(How) can we dates with format as quoted above put forward to POSIXlt? Here are sources saying that such format works (if I get them right): 1, 2.
Read ?strptime for all the details of specifying a time format. In this case you want %b (for month name), %d (day of month), %Y (4-digit year). (This will only work with an English locale setting as the month names are locale-specific.)
as.POSIXlt("May, 6 1985", format = "%b, %d %Y")
If you have mixed input of formates you can use parse_date_time from the lubridate package.
x <- c("May, 6 1985", "1990-9-1", "1/20/2012")
y <- lubridate::parse_date_time(x, c("ymd", "mdy", "dmy"))
str(y)
# POSIXct[1:3], format: "1985-05-06" "1990-09-01" "2012-01-20"
note on c("ymd", "mdy", "dmy") as this determines the order on first found first converted. Consider 6-1-2000 will encounter mdy as valid before dmy so this means it will be first of June and not sixth of January.

Problem to convert string with month abbreviation to POSIXlt [duplicate]

This question already has answers here:
strptime, as.POSIXct and as.Date return unexpected NA
(2 answers)
Closed 2 years ago.
I tried to convert Excel date character to date-time class using POSIXlt. This is an example of my data: "01:56:00 06-Apr-2017".
For the format, I used the character string giving a date-time format as used by strptime.
I tried as.POSIXlt(new_dtime, format = "%H:%M:%S %d-%b-%Y"), but it resulted in a bunch of NA. I am sure that the problem is related to the month abbreviation, despite I used %b as strptime suggests. Any help?
I'm going to take a guess that this is a locale problem: from ?strptime, %b denotes "[a]bbreviated month name in the current locale on this platform" (emphasis added). "Apr" is the abbreviation for April in an English locale. This question suggests the following solution:
str1 <- "01:56:00 06-Apr-2017"
orig_locale <- Sys.getlocale("LC_TIME")
Sys.setlocale("LC_TIME", "C")
as.POSIXct(str1, format = "%H:%M:%S %d-%b-%Y")
Sys.setlocale("LC_TIME", orig_locale)

How to convert full date character into mm/dd/yyyy in R?

I have a dataset in R with a date column that is currently being treated as a character column. The dates in this column are listed as "August 24 2012" and I am trying to convert it into a date format such as 08/24/2012.
I am a novice with R and have tried to use format() and lubridate with no success. How do I convert these dates from a character to date?
We need to convert it to Date class and then use format
format(as.Date(dates, "%B %d %Y"), "%m/%d/%Y")
#[1] "08/24/2012"
As the order is month, day, year use mdy from lubridate
library(lubridate)
format(mdy(dates), "%m/%d/%Y")
#[1] "08/24/2012"
It is based on the order, e.g.
ydm("2012 24 August")
#[1] "2012-08-24"
data
dates <- "August 24 2012"

Convert R character to date [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Converting year and month to a date in R?
I have the following string:
date <- "Feb 1964"
I am trying to convert this to a date, using the following code:
new.date <- as.Date(date, format="%b %Y")
NA is returned here. Can someone kindly explain how I can achieve this, and explain what I am doing wrong ..
Many thanks in advance
I think this might do what you want.
date <- "Feb 1964"
date <- "1 Feb 1964"
new.date <- as.Date(date, format="%d %b %Y")
new.date

Resources