I have three variables in the data (ESS):
- Age -> It is a factor with 3 values: "18-30 years", "31-65 years", "66 or more years"
- Year of the survey -> Numeric variable (2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018)
- Interest in politics -> With 4 values: 1 (Not at all interested), 2(Hardly interested), 3(Quite interested), 4(Very interested)
I want to create a new variable (interesedu) that is the mean of interest by cat_age over year. To do so, I manage to do it with Stata by the following code: egen interesedu=mean(interest), by(cat_age year)
I tried to do it in R with dplyr with the code:
ESS <-mutate(ESS,interestedu=mean(ESS$interest, na.rm = TRUE) by(ESS,ESS$cat_age, ESS$year))
But it doesnt work. Could anyone help me?