Guest User

Untitled

a guest
Mar 22nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. ``` r
  2. library(tidyverse)
  3.  
  4. gss_clean <- gss_cat %>%
  5. filter(race != "Other", marital != "No answer") %>%
  6. mutate(relig = recode(relig, Protestant = "Protestant", .default = "Not Protestant"),
  7. marital = recode(marital, Married = "Married", .default = "Not married"),
  8. race = fct_drop(race))
  9.  
  10. gss_all_3 <- gss_clean %>%
  11. group_by(marital, race, relig) %>%
  12. nest()
  13.  
  14. gss_race_marriage <- gss_clean %>%
  15. group_by(marital, race) %>%
  16. nest()
  17.  
  18. gss_marriage <- gss_clean %>%
  19. group_by(marital) %>%
  20. nest()
  21.  
  22. bind_rows(gss_all_3, gss_race_marriage, gss_marriage) %>%
  23. ungroup() %>%
  24. arrange(marital, race, relig) %>%
  25. mutate(summary = data %>% map(~ summarize(., avg_age = mean(age, na.rm = TRUE),
  26. sd_age = sd(age, na.rm = TRUE),
  27. N = n()))) %>%
  28. unnest(summary)
  29. #> # A tibble: 14 x 7
  30. #> marital race relig data avg_age sd_age N
  31. #> <fct> <fct> <fct> <list> <dbl> <dbl> <int>
  32. #> 1 Not married Black Not Protestant <tibble [672 × 6… 38.6 15.5 14
  33. #> 2 Not married Black Protestant <tibble [1,586 ×… 44.8 17.1 14
  34. #> 3 Not married Black <NA> <tibble [2,258 ×… 42.9 16.9 14
  35. #> 4 Not married White Not Protestant <tibble [4,389 ×… 44.4 18.7 14
  36. #> 5 Not married White Protestant <tibble [3,677 ×… 51.7 19.8 14
  37. #> 6 Not married White <NA> <tibble [8,066 ×… 47.7 19.5 14
  38. #> 7 Not married <NA> <NA> <tibble [10,324 … 46.7 19.1 14
  39. #> 8 Married Black Not Protestant <tibble [185 × 6… 42.8 11.6 14
  40. #> 9 Married Black Protestant <tibble [684 × 6… 47.4 13.7 14
  41. #> 10 Married Black <NA> <tibble [869 × 7… 46.4 13.4 14
  42. #> 11 Married White Not Protestant <tibble [3,810 ×… 47.8 14.6 14
  43. #> 12 Married White Protestant <tibble [4,506 ×… 51.3 15.6 14
  44. #> 13 Married White <NA> <tibble [8,316 ×… 49.7 15.2 14
  45. #> 14 Married <NA> <NA> <tibble [9,185 ×… 49.4 15.1 14
  46. ```
Add Comment
Please, Sign In to add comment