0

I have this table that I made by joining two other tables created with CPS data. I joined the two by age and sex and then I calculated the labor force participation rate for each age and sex in this hypothetical economy. Now I need to collapse the table by age and sex so that I can get the total population labor force participation rate, but for some reason the ungroup() command just isn't working. I've included an excerpt of the data in the attached image link and the code I have so far.

cps_summary2 <- left_join(cps_summary2015, cps_summary1995,
                     by = c("age", "sex"))
cps_summary2 <- cps_summary2 %>% mutate(estWorkingAgePop = cps_summary2$total_pop.x * cps_summary2$lfpr.y)
cps_summary2 <- cps_summary2 %>% mutate(laborforcerate = estWorkingAgePop/total_pop.y)  %>% 
ungroup(cps_summary2, by = c("age", "sex"))

Data Excerpt

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
gobygoul
  • 5
  • 3
  • The table seems to stay the same. – gobygoul Nov 24 '19 at 23:12
  • Actually, looking at your code, I can't see any `group_by` statement. Can you explain more in detail what your problem is, how you know that it is not working, and what you try to achieve? – coffeinjunky Nov 24 '19 at 23:12
  • I need to calculate the effect of population aging on the aggregate (full population) labor force participation rate. Specifically, what the aggregate participation rate of persons aged 15 and over have been in 2015 if the age distribution of the population were the same as it was in 1995. So I merged the two summary tables of CPS data by age and sex, multiplied the total population of 2015 by the labor force participation rates of 1995 to get the estimated working age population. Then to get the LFPR of this hypothetical i divided by the total population of 1995. I need to collapse it – gobygoul Nov 24 '19 at 23:21
  • That would then be `summarise`. See `help(dplyr)` for examples if you want to apply some function to all the values giving one return value. If this is not enough, please include some data and the desired output. – coffeinjunky Nov 24 '19 at 23:27
  • I tried that as well but the table comes up completely blank and I'm not sure why either – gobygoul Nov 24 '19 at 23:29
  • I would need data including the expected result. See here: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – coffeinjunky Nov 24 '19 at 23:30

1 Answers1

0

You have not grouped rows within the table so you cannot ungroup. You have “joined” columns from two table together on a key. To reverse that, you can simply subset the table selecting the columns you want in table 1 and assigning it to a new object; and doing the same for table 2.

i.e.

iris.1 <- iris %>%
  select(Petal.width, Petal.length)
iris.2 <- iris %>%
  select(-c(Petal.width, Petal.length))
bikeactuary
  • 447
  • 4
  • 18
  • That makes sense, but I need to collapse the labor force participation rate to get the total population rate. – gobygoul Nov 25 '19 at 00:47
  • 1
    You should back up and start with reading the dplyr documentation, looking at examples. https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html it sounds like you want to use summarize() and calculate summaries for the whole table – bikeactuary Nov 25 '19 at 04:29