0

I have the following dataframe:

hhinc_all <-pumas %>%  filter(hholder == 1) %>%
group_by(county,incgrp, hhsize, hhworker) %>%
dplyr::count(hhworker, wt = WGTP, na.rm = TRUE)

The output:

> head(hhinc_all)
# A tibble: 6 × 6
# Groups:   county, incgrp, hhsize, hhworker [6]
  county        incgrp       hhsize hhworker na.rm     n
  <chr>         <fct>        <fct>  <fct>    <lgl> <int>
1 Hardin_Orange $0 - $25,170 1      0        TRUE   5205
2 Hardin_Orange $0 - $25,170 1      1        TRUE    984
3 Hardin_Orange $0 - $25,170 2      0        TRUE   1254
4 Hardin_Orange $0 - $25,170 2      1        TRUE    664
5 Hardin_Orange $0 - $25,170 2      2+       TRUE    102
6 Hardin_Orange $0 - $25,170 3      0        TRUE    211

What I am needing is to create a three-way table that outputs to Excel in the following format:

Household-Income-Workers Three-Way Table

  • county = counties
  • incgrp = income categories
  • hhsize = households with 1 person, 2 persons, 3 persons, 4 persons, 5+ persons
  • hhworker = number of workers in a household ranging from 0 to 2+ persons
  • n = weighted values needed to populate the table

The image above comes from a sample STATA code:

table incgrp2021 hhsize hhworker county [fweight= wgtp] if hholder==1
xtable incgrp2021 hhworker hhsize [fweight= wgtp] if hholder==1, by(county) file(HHInc_3way) replace

I have tried a variety of packages to try and get the dataframe to do what I need it to, but no dice:

Ftable:

ftable(hhinc_all$county,hhinc_all$incgrp,hhinc_all$hhsize,hhinc_all$hhworker)

Crosstab from Pollster Package:

 crosstab(df = hhinc_all, x = incgrp, y = hhsize & hhworker, weight = n)

Admittedly, when creating these kinds of tables, my knowledge is limited. As far as exporting to Excel, I believe I can just summon a write command similar to write.csv and I should be fine.

Any help would be appreciated.

Phil
  • 7,287
  • 3
  • 36
  • 66
themuse
  • 3
  • 1

0 Answers0