How to Use yearmon() in R


Often you may want to extract the month and year from a date in R.

One of the best ways to do so is by using the as.yearmon() function from the zoo package in R, which can be used to perform this exact task.

The as.yearmon() function uses the following syntax:

as.yearmon(x)

where:

  • x: Name of variable with date

The following example shows how to use the as.yearmon() function in practice in R.

Example: How to Use the as.yearmon() Function in R

Suppose that we create a data frame in R that contains information about sales made on various dates by some employee at a company:

#create data frame
df <- data.frame(date=c('2023-01-15', '2023-03-19', '2023-05-20', '2023-11-25',
                        '2024-01-12', '2024-07-19', '2024-10-20', '2024-11-27'),
                 sales=c(190, 234, 280, 318, 400, 213, 299, 304))

#view data frame
df

        date sales
1 2023-01-15   190
2 2023-03-19   234
3 2023-05-20   280
4 2023-11-25   318
5 2024-01-12   400
6 2024-07-19   213
7 2024-10-20   299
8 2024-11-27   304

The date column contains the date in the format yyyy-mm-dd and the sales column contains the total number of sales made on that particular date.

Suppose that we would like to extract the month and year only for each date in the date column of the data frame.

We can use the as.yearmon() function from the zoo package to do so:

library(zoo)

as.yearmon(df$date)

[1] "Jan 2023" "Mar 2023" "May 2023" "Nov 2023" "Jan 2024" "Jul 2024" "Oct 2024"
[8] "Nov 2024"

We can see that the as.yearmon() function is able to extract the month and year for each date in the date column of the data frame.

We can also use the following syntax to store these values in a new column of the data frame named month_year:

library(zoo)

#create new column that contains year and month of each date
df$month_year <- as.yearmon(df$date)

#view updated data frame
df

        date sales month_year
1 2023-01-15   190   Jan 2023
2 2023-03-19   234   Mar 2023
3 2023-05-20   280   May 2023
4 2023-11-25   318   Nov 2023
5 2024-01-12   400   Jan 2024
6 2024-07-19   213   Jul 2024
7 2024-10-20   299   Oct 2024
8 2024-11-27   304   Nov 2024

The new column named month_year now contains the abbreviated month name and full year in 4 digits for each corresponding date in the date column of the data frame.

For example, we can see:

  • The as.yearmon() function returns Jan 2023 for 2023-01-15.
  • The as.yearmon() function returns Mar 2023 for 2023-03-19.
  • The as.yearmon() function returns May 2023 for 2023-05-20.

And so on.

It’s worth noting that the results of the as.yearmon() function are actually stored as fractional years.

If you’d like, you can wrap the as.numeric() function around the as.yearmon() function to return the numeric year and month.

You can use the following syntax to do so:

library(zoo)

#create new column that contains year and month of each date
df$month_year <- as.numeric(as.yearmon(df$date))

#view updated data frame
df

        date sales month_year
1 2023-01-15   190   2023.000
2 2023-03-19   234   2023.167
3 2023-05-20   280   2023.333
4 2023-11-25   318   2023.833
5 2024-01-12   400   2024.000
6 2024-07-19   213   2024.500
7 2024-10-20   299   2024.750
8 2024-11-27   304   2024.833

The month_year column of the data frame now contains the year and month from the date column of the data frame, expressed as a number with a decimal.

For example, we can see

  • The as.yearmon() function returns 2023.000 for 2023-01-15.
  • The as.yearmon() function returns 2023.167 for 2023-03-19.
  • The as.yearmon() function returns 2023.333 for 2023-05-20.

And so on.

Feel free to use the as.numeric() function if you would like the results of the as.yearmon() function expressed as a decimal.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Check if Column is Date in R
How to Convert Datetime to Date in R
How to Generate a Sequence of Dates in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *