I have a dataframe in R that contains the following columns structure (in a bigger scale):
Material_code actual_202009 actual_202010 actual_202011 pred_202009 pred_202010 pred_202011
111 30 44 24 25 52 27
112 19 70 93 23 68 100
I would like to add new columns to the dataframe containing the respective error measure:
|actual - pred|/ actual * 100%
Obtaining this:
Material_code actual_202009 actual_202010 actual_202011 pred_202009 pred_202010 pred_202011 MAPE_202009 MAPE_202010 MAPE_202011
111 30 44 24 25 52 27 16.67% 18.18% 12.5%
112 19 70 93 23 68 100 21.05% 2.86% 7.52%
I tried to create the new columns using ends_with()
to select the previuous, but I am not getting it right. Can you please help?
*** EDIT to include easier way to generate the dataframe
df <- data.frame(Material_code = c(111,112),
actual_202009 = c(30,19),
actual_202010 = c(44,70),
actual_202011 = c(24,93),
pred_202009 = c(25,23),
pred_202010 = c(52,68),
pred_202011 = c(27,100))