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
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"
This question already has answers here:
Create end of the month date from a date variable
(9 answers)
Closed 3 years ago.
How, in R, to get end dates of week, fortnight(beginning date too), month, quarter and year.
Let's say, we have date is "July 15th, 2017" and below code gets beginning dates.
> (a_date <- as.Date("15-07-17", "%d-%m-%y"))
[1] "2017-07-15"
> (beginning_date_week <- as.Date(cut(a_date, "week")))
[1] "2017-07-10"
> (beginning_date_month <- as.Date(cut(a_date, "month")))
[1] "2017-07-01"
> (beginning_date_quarter <- as.Date(cut(a_date, "quarter")))
[1] "2017-07-01"
> (beginning_date_year <- as.Date(cut(a_date, "year")))
[1] "2017-01-01"
adding 7 days to beginning date of week may get me the end date of week.
But, for month addition is simply not elegant(since some months have 30 days, some 31, some 28(29 some times)) and it only gets worse for quarters and years.
ceiling_date in the lubridate package returns the first date of the following period. Subtract 1 to get the last date of the current period.
library(lubridate)
a_date <- as.Date("15-07-17", "%d-%m-%y")
ceiling_date(a_date, "week", week_start = getOption("lubridate.week.start", 1))-1
[1] "2017-07-16"
ceiling_date(a_date, "month")-1
[1] "2017-07-31"
ceiling_date(a_date, "quarter")-1
[1] "2017-09-30"
ceiling_date(a_date, "year")-1
[1] "2017-12-31"
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"
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 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"