0

I’m still trying to learn how to iterate the creation of my variables, so that I don’t have to write out sets of R code for each month’s calculation of minimum wage earners. I’ve been slowly but steadily learning regular expressions, pertinent functions in R like get(), within(), paste(), apply(), assign(), and loops of course. But I’m unable to piece these concepts together to iterate the creation of variables that calculate my estimates for each month, through years 2010 to 2020. Would you be able to show me how to do that? My latest R code is below:

year2010analysis1_m1 <- year2010 %>%
  filter(hourwage == 7.25, paidhour == 2, month == 1, statefip == 13) %>%
  group_by(new_race = haven::as_factor(new_race),
           sex = haven::as_factor(sex),
           agerange = haven::as_factor(agerange)
          ) %>%
  summarize(n = sum(earnwt))

I want to iterate the creation of variable names at the last character (…_m1, …_m2, …_m3, etc.) and also iterate the numbers in the “month” argument in my filter function to be month ==1, month==2 within the next iterated variable, and so on. Shamefully, I’m still doing this manually right now ☹

Grateful for any help with this,

Ray

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • It's not clear without sample data, but if I'm inferring correctly ... you have similarly (or identically) structured data stored in `year2010`, `year2011`, ..., `year2020`, and are asking how to automate doing the same basic calculations on each of them. If that's correct, I suggest you learn about "list of frames" https://stackoverflow.com/a/24376207/3358227, and then the `lapply(.)` function. – r2evans Jan 12 '21 at 03:17
  • Please add data using `dput` or something that we can copy and use. Also show expected output for the data shared. Read about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah Jan 12 '21 at 06:44

0 Answers0