Convert range of dates in narrative form to .xts dates - r

I have a character string "April 20, 2020 - April 24, 2020" and would like to convert it to two separate strings. There are two problems here: (1) recognizing that the narrative "April" is the 4th month, and (2) that there are two different dates in this string.
I have looked at the following, plus others, but don't see my answer:
[extract dates from date range][1],
[select a range of dates][2],
[convert dates to range][3]
I've also studied "parse" but that doesn't seem to be the answer.

Would something like this work ?
string <- "April 20, 2020 - April 24, 2020"
dates <- as.Date(strsplit(string, ' - ')[[1]], '%B %d, %Y')
dates
#[1] "2020-04-20" "2020-04-24"
Or with lubridate::mdy if you don't want to remember the formats.
dates <- lubridate::mdy(strsplit(string, ' - ')[[1]])
Note that this is locale-dependent, your locale should be English.

We can use anydate
library(anytime)
anydate(strsplit(string, "\\s*-\\s*")[[1]])
#[1] "2020-04-20" "2020-04-24"

Related

Changing date format returns NA

I have a dataset with the Date column as:
Date
-------------
Aug. 21, 2022
Aug. 19, 2022
Aug. 18, 2022
...
Aug. 22, 2017
And I need the format to be in typical Date syntax i.e %d-%m-%y. However when I run the code
ftse$Date <- as.Date('Aug. 21, 2022', format = '%b-%d-%y')
ftse$Date <- format(ftse$Date, '%d-%m-%Y')
I get a column of NAs. My guess would be that the mix of a hyphen and full stop don't agree with the format function. Does anyone have any idea how to change?
Many thanks in advance.
Your format is slightly different than the one you indicate. Since you have a dot and a comma, you have to include them in the format string. As mentioned in the comments, you also need to set %Y (4-digit year) instead of %y (2-digit year).
as.Date('Aug. 21, 2022', format = '%b. %d, %Y')
#[1] "2022-08-21"
format(as.Date('Aug. 21, 2022', format = '%b. %d, %Y'), '%d-%m-%Y')
#[1] "21-08-2022"
completing akrun's answer:
install.packages("parsedate")
library(parsedate)
ftse$Date<-as.Date(parse_date(ftse$Date))

Lubridate function to convert string into date. Example strings: "21 Apr 2020" or "April 21, 2020"

I wanted to know what lubridate function can be used to convert these strings to date format.
using as_date in the above string is giving warning:
Warning message:
All formats failed to parse. No formats found
However, I am able to convert a string like this: "2020 Apr 10 11:22:23" using the as_datetime function.
With lubridate, it is just the order of day, month, year that matters. If we have multiple formats, use parse_date_time
library(lubridate)
parse_date_time(date1, orders = c('dmy', 'mdy'))
[1] "2020-04-21 UTC" "2020-04-21 UTC"
data
date1 <- c("21 Apr 2020", "April 21, 2020")
This is non-lubridate, but: if you don't know the order (d-m-y vs m-d-y vs y-m-d) in advance, or if it could be mixed within a single vector, you could try the anytime package:
anytime::anydate(c("21 Apr 2020","April 21, 2020"))
## [1] "2020-04-21" "2020-04-21"
(Apparently lubridate::parse_date_time() can handle mixed formats as well: it seems to allow slightly more control of which formats are checked for.)
It was this simple. Thank you guys :)
library(lubridate)
a <- "21 Apr 2020"
day1 <- dmy(a)
b <- "April 21, 2020"
day2 <- mdy(b)

3 letters month and 2 digits year format in R

I am trying to convert the following format to date:
as.Date('Mar.17', format = '%b.%y')
but it returns NA.
What am I missing?
Update, I am expecting to get March 2017, not 2018
it should be:
as.Date('Mar.17', format = '%b.%d')
Assuming the 17 part is the year, you could use sub to add in a day number to make it an actual date.
as.Date(sub("\\.", "01", "Mar.17"), "%b%d%y")
# [1] "2017-03-01"
as.yearmon from zoo package will do the trick and provide date(Mar 2017) as expected by OP.
library(zoo)
as.yearmon("Mar.17", "%b.%y")
#[1] "Mar 2017"
Another option to convert it to 1 March 2017
as.Date(as.yearmon("Mar.17", "%b.%y"), frac = 0)
#[1] "2017-03-01"
You need the point for %b month format, at least in my computer
as.Date(paste0( "01",'mar.2017'), format = '%d%b%Y')
"2017-03-01"

How do I convert "January 30, 2015" to a date object in R?

I have a dataframe, and one of the columns are dates that are stored in the format "month_name day, year" (e.g. "December 25, 2016"). How do I convert these to date objects? Upon searching, I came across the following code:
df$date <- as.Date(df$date, format="%m/%d/%Y")
But upon viewing the data, the date column is NA.
You can try:
as.Date("December 25, 2016", format = '%B %d, %Y')
#[1] "2016-12-25"
You need to specify the format argument in as.Date based on how your dates look like. The full details of all the options are in ?strptime.
library(lubridate)
mdy("December 25, 2016")
#[1] "2016-12-25"
Or you can use the all inclusive anytime package which handles almost all the cases.
library(anytime)
anytime("December 25, 2016")
#[1] "2016-12-25 MYT"

How to transform dates in Y-m format without days [duplicate]

This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 6 years ago.
I have a data vector that looks like this:
dates<-c("2014-11", "2014-12", "2015-01", "2015-02", "2015-03", "2015-04")
I am trying to convert it into a recognizable date format, however no luck:
as.Date(dates,"%Y-%m")
[1] NA NA NA NA NA NA
I suspect that the problem lies in that that there is no day specified.
Any thoughs of how this can be solved?
If we need to convert to Date class, it needs a day. So, we can paste with one of the days of interest, say 1, and use as.Date
as.Date(paste0(dates, "-01"))
The zoo package has a nice interface to this, which allows storing of year-month data and a as.Date method to coerce to a Date object. For example:
library("zoo")
dates <- c("2014-11", "2014-12", "2015-01", "2015-02", "2015-03", "2015-04")
The function to convert the character vector or year-months into a yearmon is as.yearmon. The second argument is the format of the date parts in the individual strings. Here I use
%Y for year with century
%m for the month as a decimal
Separated by literal -
.
yrmo <- as.yearmon(dates, "%Y-%m")
This gives
> yrmo
[1] "Nov 2014" "Dec 2014" "Jan 2015" "Feb 2015" "Mar 2015" "Apr 2015"
This is actually the default, so you can leave off the format part entirely, e.g. yrmo <- as.yearmon(dates)
To convert to a Date class object, the as.Date method is used
> as.Date(yrmo)
[1] "2014-11-01" "2014-12-01" "2015-01-01" "2015-02-01" "2015-03-01"
[6] "2015-04-01"
This method has a second argument frac which is specified allows you to state how far through the month you want each resulting Date element to be (how many days as a fraction of the length of the month in days)
> as.Date(yrmo, frac = 0.5)
[1] "2014-11-15" "2014-12-16" "2015-01-16" "2015-02-14" "2015-03-16"
[6] "2015-04-15"

Resources