By groups reescale data

Hi, i've the next data set:

structure(list(code = c("ARG", "ARG", "ARG", "ARG", "ARG", "ARG", 
"ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", "ARG", 
"ARG", "CHL", "CHL", "CHL", "CHL", "CHL", "CHL", "CHL", "CHL", 
"CHL", "CHL", "CHL", "CHL", "CHL", "CHL", "CHL", "CHL", "URY", 
"URY", "URY", "URY", "URY", "URY", "URY", "URY", "URY", "URY", 
"URY", "URY", "URY", "URY", "URY", "URY"), Year = c(2000, 2001, 
2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 
2013, 2014, 2015, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 
2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2000, 2001, 2002, 
2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 
2014, 2015), tt = c(94.16176605, 94.15779114, 94.47078705, 95.02120972, 
95.77735138, 95.67563629, 96.38970184, 98.01694489, 100.0189819, 
97.7396698, 98.29129791, 99.66351318, 100, 99.9172821, 99.38374329, 
98.87976837, 89.72451019, 88.79734802, 88.84355927, 89.22109985, 
92.50563049, 94.27552032, 102.7010117, 102.2842178, 98.90414429, 
96.30474091, 102.2522278, 102.6777725, 100, 99.70627594, 99.03490448, 
99.06980133, 100.7443085, 101.0254364, 101.2572556, 100.9225922, 
100.6803894, 99.76896667, 99.18336487, 99.25271606, 98.42314911, 
100.2634354, 100.0822449, 100.0269623, 100, 100.0033646, 100.4933853, 
101.5741577)), row.names = c(NA, -48L), groups = structure(list(
    code = c("ARG", "CHL", "URY"), .rows = structure(list(1:16, 
        17:32, 33:48), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, 3L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

I want rescale the TT index to 2010 base year (currently, the base year is 2012 = 100, i want put the 2010 = 100 as base year) in all countries (Groups). Help !!

The simplist method would be to just take 100 from all the values, I've called it tt2 here but if you wanted it to replace you could write tt before the equals sign instead.

Let me know if that helps

df %>% mutate(tt2 = tt - 100)

Thanks! but i want change the base year.

In that case, you can essentially do the same except minus the value of tt at 2010, then add 100

df %>% group_by(code) %>% mutate(tt2 = tt - mean(tt[Year==2010]) + 100)

Beware, that because I have used group_by all countries are as you asked basing on 100 for the year 2010, making the comparisons relative.

Hope that helps.

1 Like

Thanks so much. Its the solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.