title: Create a time varying and time invariant contextual variables in r
body: <p>I am attempting to do a longitudinal multilevel model using ESS survey data from 2002-2018. I want to look at the effects of macroeconomic performance on political trust. In order to do this I am using a 'within-between' approach which models a cross sectional and longitudinal component simultaneously.</p>
  <p>Right now I have a dataset in which I have added the actual inflation,GDP growth and unemployment rates for each country-year. In order to create a time invariant component, I have to create a variable which is the mean (GDP growth, inflation, unemployment), for each country over the entire time series (e.g mean GDP growth for Austria 2002-2018). However, when I run it using the code below, it creates a variable with the mean of the overall dataset for the contextual variables as opposed to the mean by country.</p>
  <pre><code>data&lt;-data%&gt;%
    group_by(country)%&gt;%
    mutate( inflation_mean = mean(inflation, na.rm = TRUE),
            inflation_diff = inflation - inflation_mean,
            growth_mean = mean(GDPgrowth, na.rm = TRUE),
            growth_diff = GDPgrowth - growth_mean,
            unemployment_mean = mean(unemployment,na.rm = TRUE),
            unemployment_diff = unemployment - unemployment_mean)
  
  
  </code></pre>
  <p>This is an example of what my data frame looks like</p>
  <pre><code>#     country  year country_year  trust  inflation GDPGrowth Unemployment
  # 1   Austria  2002  AT2002       4      2.2       4.2          4
  # 2   Austria  2002  AT2002       9      2.2       4.2          4
  # 55  Belgium  2002  BE2002       7      1.7       2.5          6
  # 56  Belgium  2002  BE2002       3      1.7       2.5          6
  # 91  Austria  2005  AT2005       2      3.4       2.9          3
  # 91  Austria  2005  AT2005       6      3.4       2.9          3
  # 141 Belgium  2005  BE2005       5      0.5       1.6          5
  # 142 Belgium  2005  BE2005       9      0.5       1.6          5
  
  </code></pre>
  <pre><code>structure(list(idno = structure(c(1, 2, 3, 4, 6, 7), format.stata = &quot;%12.0g&quot;), 
      cntry = structure(c(&quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;), format.stata = &quot;%2s&quot;), 
      essround = structure(c(1, 1, 1, 1, 1, 1), format.stata = &quot;%12.0g&quot;), 
      pspwght = structure(c(0.9409328155361, 0.47046640776805, 
      1.39215496052674, 1.38216297191755, 1.43776626993043, 1.39215496052674
      ), format.stata = &quot;%12.0g&quot;), agea = structure(c(54, 50, 63, 
      44, 41, 63), format.stata = &quot;%13.0g&quot;), gndr = structure(c(1, 
      1, 2, 1, 2, 2), format.stata = &quot;%12.0g&quot;), eduyrs = structure(c(11, 
      14, 9, 18, 15, 11), format.stata = &quot;%12.0g&quot;), mnactic = structure(c(1, 
      1, 6, 1, 1, 6), format.stata = &quot;%41.0g&quot;), lrscale = structure(c(6, 
      6, 5, 5, 5, NA), format.stata = &quot;%12.0g&quot;), rlgdgr = structure(c(8, 
      5, 7, 7, 10, 3), format.stata = &quot;%20.0g&quot;), dscrgrp = structure(c(2, 
      1, 2, 2, 2, 1), format.stata = &quot;%12.0g&quot;), dscretn = structure(c(0, 
      0, 0, 0, 0, 0), format.stata = &quot;%12.0g&quot;), ctzcntr = structure(c(1, 
      1, 1, 1, 1, 1), format.stata = &quot;%12.0g&quot;), blgetmg = structure(c(2, 
      2, NA, 2, 2, 2), format.stata = &quot;%12.0g&quot;), hincfel = structure(c(1, 
      3, 2, 1, 1, 3), format.stata = &quot;%36.0g&quot;), trstprl = structure(c(9, 
      0, 6, 8, 6, 0), format.stata = &quot;%15.0g&quot;), inwyr = structure(c(2003, 
      2003, 2003, 2003, 2003, 2003), format.stata = &quot;%13.0g&quot;), 
      inwyys = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, 
      NA_real_, NA_real_), format.stata = &quot;%13.0g&quot;), inwyye = structure(c(NA_real_, 
      NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), format.stata = &quot;%13.0g&quot;), 
      education = structure(c(11, 14, 9, 18, 15, 11), format.stata = &quot;%12.0g&quot;), 
      female = structure(c(0, 0, 1, 0, 1, 1), format.stata = &quot;%12.0g&quot;), 
      employement = structure(c(1, 1, 6, 1, 1, 6), format.stata = &quot;%41.0g&quot;), 
      age = structure(c(54, 50, 63, 44, 41, 63), format.stata = &quot;%13.0g&quot;), 
      year = c(2003, 2003, 2003, 2003, 2003, 2003), year1 = c(2010, 
      2010, 2010, 2010, 2010, 2010), year0 = c(1, 1, 1, 1, 1, 1
      ), cntry_year = c(&quot;AT 2003&quot;, &quot;AT 2003&quot;, &quot;AT 2003&quot;, &quot;AT 2003&quot;, 
      &quot;AT 2003&quot;, &quot;AT 2003&quot;), n = c(2257L, 2257L, 2257L, 2257L, 
      2257L, 2257L), year_lag = c(2002, 2002, 2002, 2002, 2002, 
      2002), gini_disp = c(26.7, 26.7, 26.7, 26.7, 26.7, 26.7), 
      unemployment = c(&quot;4.849999905&quot;, &quot;4.849999905&quot;, &quot;4.849999905&quot;, 
      &quot;4.849999905&quot;, &quot;4.849999905&quot;, &quot;4.849999905&quot;), corruption = c(1.966434, 
      1.966434, 1.966434, 1.966434, 1.966434, 1.966434), wb_growth = structure(c(1.65155392186669, 
      1.65155392186669, 1.65155392186669, 1.65155392186669, 1.65155392186669, 
      1.65155392186669), label = &quot;GDP growth (annual %)&quot;), wb_inflation = structure(c(1.81035787764132, 
      1.81035787764132, 1.81035787764132, 1.81035787764132, 1.81035787764132, 
      1.81035787764132), label = &quot;Inflation, consumer prices (annual %)&quot;), 
      old_demo = c(1, 1, 1, 1, 1, 1), gini_mean = c(28.9443587367257, 
      28.9443587367257, 28.9443587367257, 28.9443587367257, 28.9443587367257, 
      28.9443587367257), gini_diff = c(-2.24435873672569, -2.24435873672569, 
      -2.24435873672569, -2.24435873672569, -2.24435873672569, 
      -2.24435873672569), inflation_mean = c(2.36193292302435, 
      2.36193292302435, 2.36193292302435, 2.36193292302435, 2.36193292302435,   2.36193292302435), inflation_diff = structure(c(-0.551575045383031, 
      -0.551575045383031, -0.551575045383031, -0.551575045383031, 
      -0.551575045383031, -0.551575045383031), label = &quot;Inflation, consumer prices (annual %)&quot;), 
      growth_mean = c(2.11454728111128, 2.11454728111128, 2.11454728111128, 
      2.11454728111128, 2.11454728111128, 2.11454728111128), growth_diff = structure(c(-0.462993359244594, 
      -0.462993359244594, -0.462993359244594, -0.462993359244594, 
      -0.462993359244594, -0.462993359244594), label = &quot;GDP growth (annual %)&quot;), 
      corruption_mean = c(1.26648259354364, 1.26648259354364, 1.26648259354364, 
      1.26648259354364, 1.26648259354364, 1.26648259354364), corruption_diff = c(0.699951406456357, 
      0.699951406456357, 0.699951406456357, 0.699951406456357, 
      0.699951406456357, 0.699951406456357), `fdata1$cntry` = structure(c(&quot;AT&quot;, 
      &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;), format.stata = &quot;%2s&quot;), country = structure(c(&quot;AT&quot;, 
      &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;, &quot;AT&quot;), format.stata = &quot;%2s&quot;)), class = c(&quot;grouped_df&quot;, 
  &quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;), row.names = c(NA, -6L), groups = structure(list(
      `fdata1$cntry` = structure(&quot;AT&quot;, format.stata = &quot;%2s&quot;), .rows = structure(list(
          1:6), ptype = integer(0), class = c(&quot;vctrs_list_of&quot;, 
      &quot;vctrs_vctr&quot;, &quot;list&quot;))), class = c(&quot;tbl_df&quot;, &quot;tbl&quot;, &quot;data.frame&quot;
  ), row.names = c(NA, -1L), .drop = TRUE))
  
  
  
  
  
  
  </code></pre>