snap <-filter(kc_00048, countyfip ==113, age<=18)# Create a new variable for weighted count of households receiving SNAP benefitssnap <- snap %>%mutate(weighted_snap = foodstmp * perwt)# Creating new variablessnap2 <- snap %>%mutate(new_race =case_when(race ==1& hispan ==0~"Non-Hispanic White", race ==2& hispan ==0~"Black/African American", race %in%c(3,4,5,6,7,8,9) & hispan ==0~"Other", race %in%c(1,2) & hispan %in%c(1,2,3,4) ~"Hispanic", race %in%c(4,5,6) & hispan ==0~"Asian", race ==8& hispan ==0~"Two or More",TRUE~NA_character_))# Removing not reported valuessnap3<- snap2 %>%filter(new_race !="NA")# Checking the new variabletable(snap3$new_race)
Black/African American Hispanic Non-Hispanic White
295 102 73
Other
90
# Create a summary table by race and ethnicitysnap_table3 <- snap3 %>%group_by(new_race) %>%summarize(n =sum(weighted_snap))# Add a column for the total number of households in each groupsnap_table3 <- snap_table3 %>%mutate(total_hh =sum(n))# Print the summary tablesnap_table3
# A tibble: 4 × 3
new_race n total_hh
<chr> <dbl> <dbl>
1 Black/African American 119182 198110
2 Hispanic 37658 198110
3 Non-Hispanic White 19180 198110
4 Other 22090 198110
# Filter data to include only people receiving food stampsdata_foodstmp <- snap3 %>%filter(foodstmp ==2)data_foodstmp <- data_foodstmp %>%mutate(weight = perwt)# Summarize weighted estimates by race and ethnicitydata_foodstmp %>%group_by(new_race) %>%summarize(weighted_estimate =sum(weight))
# A tibble: 4 × 2
new_race weighted_estimate
<chr> <dbl>
1 Black/African American 59591
2 Hispanic 18829
3 Non-Hispanic White 9590
4 Other 11045
# Get the total weighted estimatetotal_weighted_estimate <-sum(data_foodstmp$weight)# Create a summary table by race snap_table4 <- data_foodstmp %>%group_by(new_race) %>%summarize(n =sum(weight))# Add a column for the percentage of total weighted estimatesnap_table4 <- snap_table4 %>%mutate(percentage = n / total_weighted_estimate)# Print the summary tablesnap_table4
# A tibble: 4 × 3
new_race n percentage
<chr> <dbl> <dbl>
1 Black/African American 59591 0.602
2 Hispanic 18829 0.190
3 Non-Hispanic White 9590 0.0968
4 Other 11045 0.112