How to convert full date character into mm/dd/yyyy in R? - 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"

Related

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.

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

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"

Unable to convert Month-Year string to Date in R

I'm using as.Date to convert a string like Aug-2002 to a dates object representing just the month of Aug of 2002, or if a day must be specified, Aug 1, 2002.
However
> as.Date(c('07-2002'), "%M-%Y")
[1] "2002-11-06"
> as.Date(c('Aug-2002'), "%b-%Y")
[1] NA
Why does the first line of code convert it to a different month and day? And the second one is NA?
I referred to this table for the formatting symbols.
The problem you are having is that the dates you have do not have a day value. Without the day value the format="%m-%Y" will not work in as.Date. These options below will solve them:
as.Date(paste0('01-', c('07-2002')), format="%d-%m-%Y")
library(zoo) #this is a little more forgiving:
as.yearmon(c('07-2002'), "%m-%Y")
as.yearmon(c('Aug-2002'), "%b-%Y")
as.Date(as.yearmon(c('07-2002'), "%m-%Y"))

R format of date trouble

I'm confused as to why this as.Date("201410", "%Y%m") is not converted to a date... That is, I expect that the format article of the function as.Date in the example would take the "201410" and convert it to date.
Any help?
If you are willing to make the assumption that these dates are on average occurring at the middle of a month then this would be a dodgy way of using the Date class:
as.Date(paste0("201410", "01"), "%Y%m%d")
#[1] "2014-10-01"
This is how to create a yearmon object:
> require(zoo)
> as.yearmon("201410","%Y%m")
[1] "Oct 2014"

Formatting month abbreviations using as.Date [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
I'm working with monthly data and have a character vector of dates, formatted:
Sep/2012
Aug/2012
Jul/2012
and so on, back to 1981. I've tried using
as.Date(dates, "%b/%Y")
where %b represents month abbreviations, but this only returns NAs. What am I doing wrong?
Note: I already found a workaround using gsub() to add "01/" in front of each entry, like so:
01/Sep/2012
01/Aug/2012
01/Jul/2012
Then as.Dates() works, but this seems a little inelegant, and isn't strictly accurate anyway.
You are looking for as.yearmon() in the zoo package. Given your dates
dates <- c("Sep/2012","Aug/2012","Jul/2012")
we load the package and convert to the "yearmon" class
require(zoo)
dates1 <- as.yearmon(dates, format = "%b/%Y")
dates1
Which gives
R> dates1
[1] "Sep 2012" "Aug 2012" "Jul 2012"
You can coerce to an object of class "Date" using the as.Date() method
R> as.Date(dates1)
[1] "2012-09-01" "2012-08-01" "2012-07-01"
Which would be a simpler way of getting the thing you did via gsub(). There is a frac argument which controls how far through the month the day component should be:
R> as.Date(dates1, frac = 0.5)
[1] "2012-09-15" "2012-08-16" "2012-07-16"
But that may ont be sufficient for you.
If you really only want the dates stored as you have them, then they aren't really dates but if you are happy to work within the zoo package then the "yearmon" class can be used as an index for a zoo object which is a time series.

Resources