How can I extract the month from a date in R? Can you provide some examples?

How can I extract the month from a date in R? Can you provide some examples?

In order to extract the month from a given date in R, one can use the “month” function which is part of the “lubridate” package. This function allows for easy extraction of the month component from a date object, and it can be used with various date formats. For example, if the date is in the format of “YYYY-MM-DD”, the month function will return the numerical representation of the month (e.g. “01” for January). Similarly, if the date is in the format of “DD/MM/YYYY”, the month function will return the numerical representation of the month (e.g. “12” for December). Additional examples and instructions on how to use the “month” function can be found in the documentation of the “lubridate” package.

Extract Month from Date in R (With Examples)


There are two ways to quickly extract the month from a date in R:

Method 1: Use format()

df$month <- format(as.Date(df$date, format="%d/%m/%Y"),"%m")

Method 2: Use the lubridate package

library(lubridate)

df$month <- month(mdy(df$date))

This tutorial shows an example of how to use each of these methods in practice.

Method 1: Extract Month from Date Using format()

The following code shows how to extract the month from a date using the format() function combined with the “%m” argument:

#create data frame
df <- data.frame(date=c("01/01/2021", "01/04/2021" , "01/09/2021"),
                  sales=c(34, 36, 44))

#view data frame
df

        date sales
1 01/01/2021    34
2 01/04/2021    36
3 01/09/2021    44

#create new variable that contains month
df$month <- format(as.Date(df$date, format="%d/%m/%Y"),"%m")

#view updated data frame
df

        date sales month
1 01/01/2021    34    01
2 01/04/2021    36    04
3 01/09/2021    44    09

Note that this format() function works with a variety of date formats. You simply must specify the format:

#create data frame
df <- data.frame(date=c("2021-01-01", "2021-01-04" , "2021-01-09"),
                  sales=c(34, 36, 44))

#view data frame
df

        date sales
1 2021-01-01    34
2 2021-01-04    36
3 2021-01-09    44

#create new variable that contains month
df$month<- format(as.Date(df$date, format="%Y-%m-%d"),"%m")

#view updated data frame
df

        date sales month
1 2021-01-01    34    01
2 2021-01-04    36    01
3 2021-01-09    44    01

Note: You can also use %B to extract the month as a string name (January) instead of a numeric value (01).

Method 2: Extract Month from Date Using Lubridate

We can also use functions from the lubridate package to quickly extract the month from a date:

library(lubridate)

#create data frame
df <- data.frame(date=c("01/01/2021", "01/04/2021" , "01/09/2021"),
                  sales=c(34, 36, 44))

#view data frame
df

        date sales
1 01/01/2021    34
2 01/04/2021    36
3 01/09/2021    44

#create new variable that contains month
df$month<- month(mdy(df$date))

#view updated data frame
df

        date sales month
1 01/01/2021    34     1
2 01/04/2021    36     1
3 01/09/2021    44     1

Lubridate also works with a variety of date formats. You simply must specify the format:

#create data frame
df <- data.frame(date=c("2021-01-01", "2021-01-04" , "2021-01-09"),
                  sales=c(34, 36, 44))

#view data frame
df

        date sales
1 2021-01-01    34
2 2021-01-04    36
3 2021-01-09    44

#create new variable that contains month
df$month <- month(ymd(df$date))

#view updated data frame
df

        date sales month
1 2021-01-01    34     1
2 2021-01-04    36     1
3 2021-01-09    44     1

Additional Resources

Cite this article

stats writer (2024). How can I extract the month from a date in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-from-a-date-in-r-can-you-provide-some-examples/

stats writer. "How can I extract the month from a date in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 28 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-from-a-date-in-r-can-you-provide-some-examples/.

stats writer. "How can I extract the month from a date in R? Can you provide some examples?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-from-a-date-in-r-can-you-provide-some-examples/.

stats writer (2024) 'How can I extract the month from a date in R? Can you provide some examples?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-extract-the-month-from-a-date-in-r-can-you-provide-some-examples/.

[1] stats writer, "How can I extract the month from a date in R? Can you provide some examples?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How can I extract the month from a date in R? Can you provide some examples?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top