Home > OS >  Is there any function that give the changes between columns?
Is there any function that give the changes between columns?

Time:06-12

I have a df that looks like this.

head(dfhigh)
        rownames       2015Y     2016Y     2017Y     2018Y     2019Y      2020Y     2021Y
1      Australia  29583.7403 48397.383 45220.323 68461.941 39218.044  20140.351 29773.188
2       Austria*   1294.5092 -8400.973 14926.164  5511.625  2912.795 -14962.963  5855.014
3       Belgium* -24013.3111 68177.596 -3057.153 27119.084 -9208.553  13881.481 22955.298
4         Canada  43852.7732 36061.859 22764.156 37653.521 50141.784  23174.006 59693.992
5         Chile*  20507.8407 12249.294  6128.716  7735.778 12499.238   8385.907 15251.538
6 Czech Republic    465.2137  9814.496  9517.948 11010.423 10108.914   9410.576  5805.084

I want to calculate the changes between years, so instead of the values, the table has the percentage of change (obviously deleting 2015Y).

CodePudding user response:

Try this using (current - previous)/ previous *100

lst <- list()

nm <- names(dfhigh)[-1]

for(i in 1:(length(nm) - 1)){
  lst[[i]] <- (dfhigh[[nm[i 1]]] - dfhigh[[nm[i]]]) / dfhigh[[nm[i]]] * 100
}

ans <- do.call(cbind , lst)
colnames(ans) <- paste("ch_of" , nm[-1])

ans

you can change the formula to calculate percentage as you want

CodePudding user response:

You could also use a tidyverse solution.

library(tidyverse)

df %>%
  pivot_longer(!rownames) %>%
  group_by(rownames) %>%
  mutate(value = 100*value/lag(value)-100) %>%
  ungroup() %>%
  pivot_wider(names_from = name, values_from = value)

# # A tibble: 6 × 8
#   rownames      `2015Y` `2016Y` `2017Y` `2018Y` `2019Y` `2020Y` `2021Y`
#   <chr>           <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
# 1 Australia          NA    63.6   -6.56    51.4  -42.7   -48.6     47.8
# 2 Austria*           NA  -749.  -278.     -63.1  -47.2  -614.    -139. 
# 3 Belgium*           NA  -384.  -104.    -987.  -134.   -251.      65.4
# 4 Canada             NA   -17.8  -36.9     65.4   33.2   -53.8    158. 
# 5 Chile*             NA   -40.3  -50.0     26.2   61.6   -32.9     81.9
# 6 CzechRepublic      NA  2010.    -3.02    15.7   -8.19   -6.91   -38.3
  • Related