Search code examples
rlubridate

Mutate Year with Month Column for a Time Series Data Input in R Using Lubridate Package


I have this time series data frame as follows:

df <- read.table(text = 
                     "Year Month Value
2021  1        4
2021  2       11
2021  3       18
2021  4        6
2021  5       20
2021  6        5
2021  7       12
2021  8        4
2021  9       11
2021  10      18
2021  11       6
2021  12      20
2022  1       14
2022  2       11
2022  3       18
2022  4        9
2022  5       22
2022  6       19
2022  7       22
2022  8       24
2022  9       17
2022  10      28
2022  11      16
2022  12      26",
header = TRUE)

I want to turn this data frame into a time series object of date column and value column only so that I can use the ts function to filter the starting point and the endpoint like ts(ts, start = starts, frequency = 12). R should know that 2022 is a year and the corresponding 1:12 are its months, the same thing should apply to 2021. I will prefer lubridate package.

pacman::p_load(
dplyr,
lubridate)

UPDATE

I now use unite function from dplyr package.

df|>
  unite(col='date', c('Year', 'Month'), sep='')

Solution

  • Perhaps this?

    df |>
      tidyr::unite(col='date', c('Year', 'Month'), sep='-') |>
      mutate(date = lubridate::ym(date))
    #          date Value
    # 1  2021-01-01     4
    # 2  2021-02-01    11
    # 3  2021-03-01    18
    # 4  2021-04-01     6
    # 5  2021-05-01    20
    # 6  2021-06-01     5
    # 7  2021-07-01    12
    # 8  2021-08-01     4
    # 9  2021-09-01    11
    # 10 2021-10-01    18
    # 11 2021-11-01     6
    # 12 2021-12-01    20
    # 13 2022-01-01    14
    # 14 2022-02-01    11
    # 15 2022-03-01    18
    # 16 2022-04-01     9
    # 17 2022-05-01    22
    # 18 2022-06-01    19
    # 19 2022-07-01    22
    # 20 2022-08-01    24
    # 21 2022-09-01    17
    # 22 2022-10-01    28
    # 23 2022-11-01    16
    # 24 2022-12-01    26