Extract month and year from a zoo::yearmon object
Use the format()
method for objects of class "yearmon"
. Here is your example date (properly created!)
date1 <- as.yearmon("Mar 2012", "%b %Y")
Then we can extract the date parts as required:
> format(date1, "%b") ## Month, char, abbreviated
[1] "Mar"
> format(date1, "%Y") ## Year with century
[1] "2012"
> format(date1, "%m") ## numeric month
[1] "03"
These are returned as characters. Where appropriate, wrap in as.numeric()
if you want the year or numeric month as a numeric variable, e.g.
> as.numeric(format(date1, "%m"))
[1] 3
> as.numeric(format(date1, "%Y"))
[1] 2012
See ?yearmon
and ?strftime
for details - the latter explains the placeholder characters you can use.
Identify fiscal year for yearmon object in R
We assume that the fiscal year ends in September so that the fiscal year corresponding to October, November and December is the following calendar year and for other months the fiscal year is the same as the calendar year.
Push the input forward by three months by adding 3/12 to the input yearmon
object so that October, November and December get pushed into the next calendar year but no other month is and then format:
library(zoo)
ym <- yearmon(2020 + 0:11/12) # test data: Jan '20, Feb '20, ..., Dec '20
format(ym + 3/12, "FY%y")
## [1] "FY20" "FY20" "FY20" "FY20" "FY20" "FY20" "FY20" "FY20" "FY20" "FY21"
## [11] "FY21" "FY21"
Extract Month and Year From Date in R
This will add a new column to your data.frame
with the specified format.
df$Month_Yr <- format(as.Date(df$Date), "%Y-%m")
df
#> ID Date Month_Yr
#> 1 1 2004-02-06 2004-02
#> 2 2 2006-03-14 2006-03
#> 3 3 2007-07-16 2007-07
# your data sample
df <- data.frame( ID=1:3,Date = c("2004-02-06" , "2006-03-14" , "2007-07-16") )
a simple example:
dates <- "2004-02-06"
format(as.Date(dates), "%Y-%m")
> "2004-02"
side note:
the data.table
approach can be quite faster in case you're working with a big dataset.
library(data.table)
setDT(df)[, Month_Yr := format(as.Date(Date), "%Y-%m") ]
Extract month and year from datetime in R
lubridate
month
and year
will work.
as.data.frame(Order.Date) %>%
mutate(Month = lubridate::month(Order.Date, label = FALSE),
Year = lubridate::year(Order.Date))
Order.Date Month Year
1 2011-10-20 10 2011
2 2011-12-25 12 2011
3 2012-04-15 4 2012
4 2012-08-23 8 2012
5 2013-09-25 9 2013
If you want month format as Jan
, use month.abb
and as January
, use month.name
as.data.frame(Order.Date) %>%
mutate(Month = month.abb[lubridate::month(Order.Date, label = TRUE)],
Year = lubridate::year(Order.Date))
Order.Date Month Year
1 2011-10-20 Oct 2011
2 2011-12-25 Dec 2011
3 2012-04-15 Apr 2012
4 2012-08-23 Aug 2012
5 2013-09-25 Sep 2013
as.data.frame(Order.Date) %>%
mutate(Month = month.name[lubridate::month(Order.Date, label = TRUE)],
Year = lubridate::year(Order.Date))
Order.Date Month Year
1 2011-10-20 October 2011
2 2011-12-25 December 2011
3 2012-04-15 April 2012
4 2012-08-23 August 2012
5 2013-09-25 September 2013
Converting yearmon column to last date of the month in R
If the Date
variable is an actual yearmon
class vector, from the zoo package, the as.Date.yearmon
method can do what you want via its argument frac
.
Using your data, and assuming that the Date
was originally a character vector
library("zoo")
df <- data.frame(Date = c("2014-07", "2014-08", "2014-09"),
Arrivals = c(100, 150, 200))
I convert this to a yearmon
vector:
df <- transform(df, Date2 = as.yearmon(Date))
Assuming this is what you have, then you can achieve what you want using as.Date()
with frac = 1
:
df <- transform(df, Date3 = as.Date(Date2, frac = 1))
which gives:
> df
Date Arrivals Date2 Date3
1 2014-07 100 Jul 2014 2014-07-31
2 2014-08 150 Aug 2014 2014-08-31
3 2014-09 200 Sep 2014 2014-09-30
That shows the individual steps. If you only want the final Date
this is a one-liner
## assuming `Date` is a `yearmon` object
df <- transform(df, Date = as.Date(Date, frac = 1))
## or if not a `yearmon`
df <- transform(df, Date = as.Date(as.yearmon(Date), frac = 1))
The argument frac
in the fraction of the month to assign to the resulting dates when converting from yearmon
objects to Date
objects. Hence, to get the first day of the month, rather than convert to a character and paste on "-01"
as your Question showed, it's better to coerce to a Date
object with frac = 0
.
If the Date
in your df
is not a yearmon
class object, then you can solve your problem by converting it to one and then using the as.Date()
method as described above.
Extract Month from Week and Year number
This extracts the month of the first day of the week, you could add a constant or something if you wanted the 2nd or 3rd day of the week instead.
df %>%
mutate(Month = month(ymd(Year * 10000 + 0101) + Week * 7))
Related Topics
Painless Way to Install a New Version of R
Using Regex in R to Find Strings as Whole Words (But Not Strings as Part of Words)
Selecting Only Numeric Columns from a Data Frame
Split Date-Time Column into Date and Time Variables
How to Number/Label Data-Table by Group-Number from Group_By
A Similar Function to R'S Rep in Matlab
Find How Many Times Duplicated Rows Repeat in R Data Frame
How to Install Packages in Latest Version of Rstudio and R Version.3.1.1
What Does %≫% Function Mean in R
Format Numbers With Million (M) and Billion (B) Suffixes
Finding Running Maximum by Group
How to Sum a Numeric List Elements