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"
Related
I am trying to get the duration between issue_d and last_pymnt_d in my dataset using the lubridate package. issue_d is in the following formatting chr "2015-05-01T00:00:00Z" and last_pymnt_d is in chr "Feb-2017". I need them in the same format (just need "my" or "myd" is fine if "my" is not an option) Then I need to know calculate between issue_d and last_pymnt_d.
lcDataSet2$issue_d<-parse_date_time(lcDataSet2$issue_d, "myd")
turns my issue_d into NA. I also get the below error when even just trying to view last_pymnt_d in date format
as.Date(lcRawData$last_pymnt_d)
Error in charToDate(x) :
character string is not in a standard unambiguous format
How can I get these into the same date format and then calculate the duration?
The order and letter case of the format string is important for parsing dates.
library(lubridate)
parse_date_time('2015-05-01T00:00:00Z', 'Y-m-d H:M:S')
[1] "2015-05-01 UTC"
parse_date_time('Feb-2017', 'b-Y')
[1] "2017-02-01 UTC"
If wanting just the month and year there is a zoo function
library(zoo)
date1 <- as.yearmon('2015-05-01T00:00:00Z')
[1] "May 2015"
date2 <- as.yearmon('Feb-2017', '%b-%Y')
[1] "Feb 2017"
difftime(date2, date1)
Time difference of 642 days
The zoo package gives you a function as.yearmon to covert dates into yearmon objects containing only the month and year. Since your last_pymnt_d is only month and year, the best date difference you will get is number of months:
library(zoo)
issue_d <- "2015-05-01T00:00:00Z"
last_pymnt_d <- "Feb-2017"
diff <- as.yearmon(last_pymnt_d, format = "%b-%Y") - as.yearmon(as.Date(issue_d))
diff
1.75
Under the hood, the yearmon object is a number of years, with the decimal component representing the months. A difference in yearmon of 1.75 is 1 year and 9 months.
diff_months <- paste(round(diff * 12, 0), "months")
"21 months"
diff_yearmon <- paste(floor(diff), "years and", round((diff %% 1) * 12, 0), "months")
diff_yearmon
"1 years and 9 months"
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"
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)
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 5 years ago.
Is it possible to format the following number to Year-Month
I entries as follows:
1402
1401
1312
Meaning February 2014. January 2014 and December 2013.
I tried:
date <- 1402
date <- as.Date(as.character(date), format = "%y%m")
But I get an NA as an output.
The zoo package has a "yearmon" class that directly handles year/month objects:
library(zoo)
nums <- c(1402, 1401, 1312)
ym <- as.yearmon(as.character(nums), "%y%m")
giving:
> ym
[1] "Feb 2014" "Jan 2014" "Dec 2013"
You need to include day number, otherwise it is impossible to understand what day of month you have in mind, consider:
> strptime('011402', format = "%d%y%m")
[1] "2014-02-01"
as.Date requires a full date, with day specified. Since you don't include a day it doesn't know what to do.
You could add any day and it should work like this
date <- 140201
date <- as.Date(as.character(date), format="%y%m%d")
You could use the lubridate package to work with date a little bit easier.
> library(lubridate)
> month(ymd(as.character(140201), label=TRUE)
[1] February
This question already has answers here:
Converting year and month ("yyyy-mm" format) to a date?
(9 answers)
Closed 5 years ago.
Is it possible to format the following number to Year-Month
I entries as follows:
1402
1401
1312
Meaning February 2014. January 2014 and December 2013.
I tried:
date <- 1402
date <- as.Date(as.character(date), format = "%y%m")
But I get an NA as an output.
The zoo package has a "yearmon" class that directly handles year/month objects:
library(zoo)
nums <- c(1402, 1401, 1312)
ym <- as.yearmon(as.character(nums), "%y%m")
giving:
> ym
[1] "Feb 2014" "Jan 2014" "Dec 2013"
You need to include day number, otherwise it is impossible to understand what day of month you have in mind, consider:
> strptime('011402', format = "%d%y%m")
[1] "2014-02-01"
as.Date requires a full date, with day specified. Since you don't include a day it doesn't know what to do.
You could add any day and it should work like this
date <- 140201
date <- as.Date(as.character(date), format="%y%m%d")
You could use the lubridate package to work with date a little bit easier.
> library(lubridate)
> month(ymd(as.character(140201), label=TRUE)
[1] February