Year and month combination in R

Year and month combination in R

The year and month combination in R is possible in different ways. In my case, it is a combination in one data frame column that is logically sortable. For example, the combination in a format YYYYMM.

For the following example, I generated a list of dates like this.

x <-
  data.frame("my_date" = seq.Date(
    from = as.Date("2021-12-29"),
    to = as.Date("2022-01-03"),
    by = "days"
  ))

x

#     my_date
#1 2021-12-29
#2 2021-12-30
#3 2021-12-31
#4 2022-01-01
#5 2022-01-02
#6 2022-01-03

By using the lubridate package, I will extract the year and month in separate columns.

require(lubridate)

x$year <- year(x$my_date)
x$month <- month(x$my_date)

x

#     my_date year month
#1 2021-12-29 2021    12
#2 2021-12-30 2021    12
#3 2021-12-31 2021    12
#4 2022-01-01 2022     1
#5 2022-01-02 2022     1
#6 2022-01-03 2022     1

 

Year and month combination in R

Here is how to get the year and month combination in R mathematically. The result is numeric.

x$year * 100 + x$month

#[1] 202112 202112 202112 202201 202201 202201

By using the function mutate, you can do that with help of dplyr capabilities.

require(dplyr)

x %>%
  mutate("yearmonth" = year * 100 + month)


#     my_date year month yearmonth
#1 2021-12-29 2021    12    202112
#2 2021-12-30 2021    12    202112
#3 2021-12-31 2021    12    202112
#4 2022-01-01 2022     1    202201
#5 2022-01-02 2022     1    202201
#6 2022-01-03 2022     1    202201

The other approach is by using concatenation. The result is a text. It is necessary to add leading zeros to the month number where necessary.

paste0(x$year, sprintf("%02d", x$month))

#[1] "202112" "202112" "202112" "202201" "202201" "202201"

If you want a separator between year and date as it is, you can extract the necessary part as a text from the date or use a concatenation as previously.

substr(x$my_date, 0, 7)

#[1] "2021-12" "2021-12" "2021-12" "2022-01" "2022-01" "2022-01"

 

Year and month combined into a date in R

Let’s say you want to get the first date of the month. You can join a year, month, and a number that represents the first day like this.

as.Date(paste(x$year, sprintf("%02d", x$month), 1, sep = "-"))

#[1] "2021-12-01" "2021-12-01" "2021-12-01" "2022-01-01" "2022-01-01" "2022-01-01"

If you are getting year and month from the date, then there might be easier to get the date of the first day of the month.

lubridate::floor_date(x$my_date, unit = "month")

#[1] "2021-12-01" "2021-12-01" "2021-12-01" "2022-01-01" "2022-01-01" "2022-01-01"

 

Year and week combination in R

If you want to combine year and week instead of year and month, keep in mind that the best way might be ISO year and ISO week number calculation. By using ISO week, there will be 7 days in every week and, as a result, a more objective comparison.





Posted

in

Comments

Leave a Reply

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