Creating a new variable with by()

I have three variables in the data (ESS):

  1. Age -> It is a factor with 3 values: "18-30 years", "31-65 years", "66 or more years"
  2. Year of the survey -> Numeric variable (2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018)
  3. 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?

1 Like

What source have you used to learn dplyr syntax?
My recommendation would be r4ds

Yes, I use that book

1 Like

Use the verb group_by():

ESS %>% 
group_by(cat_age, year) %>%
mutate(interestedu=mean(ESS$interest, na.rm = TRUE))
1 Like

This topic was automatically closed 21 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.