0

My code to summarise data looks like this:

sec_02 <- employment_mf_02 %>%
  select(sex, industry, hhwt) %>% 
  group_by(industry) %>% 
  count(industry, sex, wt = hhwt) %>% 
  pivot_wider(names_from = "sex", values_from = "n") %>% 
  mutate(Industry_sum = Male + Female,
         Msec = Male / Industry_sum,
         Fsec = Female / Industry_sum)

sec_04 <- employment_mf_04 %>%
  select(sex, industry, hhwt) %>% 
  group_by(industry) %>% 
  count(industry, sex, wt = hhwt) %>% 
  pivot_wider(names_from = "sex", values_from = "n") %>% 
  mutate(Industry_sum = Male + Female,
         Msec = Male / Industry_sum,
         Fsec = Female / Industry_sum)

sec_06 <- employment_mf_06 %>%
  select(sex, industry, hhwt) %>% 
  group_by(industry) %>% 
  count(industry, sex, wt = hhwt) %>% 
  pivot_wider(names_from = "sex", values_from = "n") %>% 
  mutate(Industry_sum = Male + Female,
         Msec = Male / Industry_sum,
         Fsec = Female / Industry_sum)

I understand that I can put employment_mf_xx in a list and use a for-loop to do this. However, this would change the underlying data, but I want to create new dataframes sec_02 sec_04 and sec_06. Is there a way I can do this?

Thank you.

anrisakaki96
  • 193
  • 8
  • Are all the dataframes exactly the same? ie same Column names? – Onyambu Apr 20 '22 at 14:23
  • Yep, they all have the same column names (but different values) – anrisakaki96 Apr 20 '22 at 14:24
  • 1
    Try `fun <- . %>% select(sex, industry, hhwt) %>% group_by(industry) %>% count(industry, sex, wt = hhwt) %>% pivot_wider(names_from = "sex", values_from = "n") %>% mutate(Industry_sum = Male + Female, Msec = Male / Industry_sum, Fsec = Female / Industry_sum); lapply(mget(ls(pattern = "^employment_mf_")), fun)` – G. Grothendieck Apr 20 '22 at 14:29
  • Consider using lists from the start especially for similarly structured objects. See this [canonical answer](https://stackoverflow.com/a/24376207/1422451): *Don't ever create `d1` `d2` `d3`, ..., `dn` in the first place. Create a list `d` with `n` elements.* – Parfait Apr 20 '22 at 14:29

1 Answers1

1
list(sec02 = employment_mf_02, sec04 = employment_mf_04, sec06 = employment_mf_06) %>%
  bind_rows(.id = 'grp') %>%
  select(grp, sex, industry, hhwt) %>% 
  group_by(grp, industry) %>% 
  count(industry, sex, wt = hhwt) %>% 
  pivot_wider(names_from = "sex", values_from = "n") %>% 
  mutate(Industry_sum = Male + Female,
         Msec = Male / Industry_sum,
         Fsec = Female / Industry_sum) %>%
  ungroup() %>%
  group_by(grp) %>%
  group_split(.keep = FALSE) %>%
  list2env(.GlobalEnv)

Now call sec02

Onyambu
  • 67,392
  • 3
  • 24
  • 53