ciao guys,
i have the following dataframe
obj <- data.frame (degree2 = c(1, 1, 2, 2, 3, 3, 4, 4),
yr = c(1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997),
degree = c(1, 1, 1, 2, 1, 1, 0, 0), degree3 = c(1, 1, 6, 7, 5, 1, 0, 0)
)
what I would like to do is to create the percentage change for the variables degree
degree2
degree3
per year. Note that my real dataframe is quite long.
I guess the code must by something like in the sense of: "for every row compute percentual change for all variables except yr
many thanks in advance!
Update with correct function to calculate the percentage change:
Thanks to ThoamslsCoding:
The function to calculate:
Divide x
by the lag
of x
substract 1
and then multiple by 100
pct_change <- function(x) {(x/lag(x) -1) * 100}
obj %>%
mutate(across(c(degree2, degree, degree3), pct_change, .names = "pct_change_{.col}"))
Output:
degree2 yr degree degree3 pct_change_degree2 pct_change_degree pct_change_degree3
1 1 1990 1 1 NA NA NA
2 1 1991 1 1 0.0 0 0.0
3 2 1992 1 6 100.0 0 500.0
4 2 1993 2 7 0.0 100 16.7
5 3 1994 1 5 50.0 -50 -28.6
6 3 1995 1 1 0.0 0 -80.0
7 4 1996 0 0 33.3 -100 -100.0
8 4 1997 0 0 0.0 NaN NaN
First answer: not correct:
# function to calculate percentage change
pct_change <- function(x) {x/lag(x)}
obj %>%
mutate(across(c("degree2", "degree", "degree3"), pct_change))
output:
degree2 yr degree degree3 degree2_perc_change degree_perc_change degree3_perc_change
1 NA 1990 NA NA 0.0 0 0.0
2 1.00 1991 1.0 1.000 0.0 0 0.0
3 2.00 1992 1.0 6.000 50.0 0 83.3
4 1.00 1993 2.0 1.167 0.0 50 14.3
5 1.50 1994 0.5 0.714 33.3 -100 -40.0
6 1.00 1995 1.0 0.200 0.0 0 -400.0
7 1.33 1996 0.0 0.000 25.0 -Inf -Inf
8 1.00 1997 NaN NaN 0.0 NaN NaN
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With