ah bon
ah bon

Reputation: 10021

Calculate monthly and yearly percentage changes of multiple columns in R

I have a dataframe df as follows:

df <- read.table(text = "date    value1    value2
           2019-01        0.7         1.4
           2019-02        0.11        2.3
           2019-03        1.22        6.7
           2019-04        0.44        5.2
           2019-05        0.19        2.3
           2019-06        3.97        9.5
           2019-07        1.24        2.4
           2019-08        0.23        2.88
           2019-09        0.66        9.5
           2019-10        1.24        2.2
           2019-11        0.23        2.28
           2019-12        0.66        9.3
           2020-01        0.6         1.2
           2020-02        0.13        2.1
           2020-03        1.12        6.4
           2020-04        0.34        5.1
           2020-05        0.29        2.1
           2020-06        3.47        9
           2020-07        1.44        2.1
           2020-08        0.33        2.5
           2020-09        0.76        9.2
           2020-07        1.28        2.43
           2020-08        0.25        2.8
           2020-09        0.69        9.5",
                 header = TRUE, stringsAsFactors = FALSE)

How could I calculate monthly pct_changes and yearly pct_changes in for columns value1 and value2 in R? Thanks.

In pandas, we can use apply(lambda: x.pct_change()) for month_pct and apply(lambda: x.pct_change(12)) for year_pct, I don't know if in R having similar function or not.

Upvotes: 0

Views: 790

Answers (2)

AlexB
AlexB

Reputation: 3269

Your date column is character, it is better to convert it to a Date object. Use lag function from dplyr package to perform the computations.

This is for value1 column.

 df %>%
    mutate(date = lubridate::ymd(paste0(date, '-01'))) %>%
    mutate(monthly_pct_changes = round((value1 / lag(value1) - 1) * 100, 2),
           yearly_pct_changes = round((value1 / lag(value1, 12) - 1) * 100, 2))

Repeat the same procedure for the other column.

Upvotes: 2

Ronak Shah
Ronak Shah

Reputation: 388982

You can use lag to access previous row's value :

library(dplyr)

df %>%
  mutate(across(c(value1,value2), list(monthly = ~(. - lag(.))/lag(.), 
                                       yearly = ~(. - lag(., 12))/lag(., 12))))

#      date value1 value2 value1_monthly value1_yearly value2_monthly value2_yearly
#1  2019-01   0.70   1.40             NA            NA             NA            NA
#2  2019-02   0.11   2.30    -0.84285714            NA     0.64285714            NA
#3  2019-03   1.22   6.70    10.09090909            NA     1.91304348            NA
#4  2019-04   0.44   5.20    -0.63934426            NA    -0.22388060            NA
#...

Upvotes: 1

Related Questions