Guest User

Untitled

a guest
Jan 20th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. titanic_4 <- titanic %>%
  2. select(Survived, Pclass, Age, Sex) %>%
  3. filter(!is.na(Age)) %>%
  4. mutate(agecat = cut(Age, breaks = c(0, 14.99, 50, 150),
  5. include.lowest = TRUE,
  6. labels = c("Under 15", "15 to 50",
  7. "Over 50"))) %>%
  8. group_by(Pclass,agecat,Sex) %>%
  9. summarize(N=n(), survivors = sum(Survived))%>%
  10. mutate(perc_survived = (signif((100*survivors/N), digits=8)))
  11.  
  12. print(titanic_4)
  13.  
  14. # A tibble: 18 x 6
  15. # Groups: Pclass, agecat [9]
  16. Pclass agecat Sex N survivors perc_survived
  17. <int> <fctr> <chr> <int> <int> <dbl>
  18. 1 1 Under 15 female 2 1 50.000000
  19. 2 1 Under 15 male 3 3 100.000000
  20. 3 1 15 to 50 female 70 68 97.142857
  21. 4 1 15 to 50 male 72 32 44.444444
  22. 5 1 Over 50 female 13 13 100.000000
  23. 6 1 Over 50 male 26 5 19.230769
  24. 7 2 Under 15 female 10 10 100.000000
  25. 8 2 Under 15 male 9 9 100.000000
  26. 9 2 15 to 50 female 61 56 91.803279
  27. 10 2 15 to 50 male 78 5 6.410256
  28. 11 2 Over 50 female 3 2 66.666667
  29. 12 2 Over 50 male 12 1 8.333333
  30. 13 3 Under 15 female 27 13 48.148148
  31. 14 3 Under 15 male 27 9 33.333333
  32. 15 3 15 to 50 female 74 33 44.594595
  33. 16 3 15 to 50 male 217 29 13.364055
  34. 17 3 Over 50 female 1 1 100.000000
  35. 18 3 Over 50 male 9 0 0.000000
  36.  
  37. # A tibble: 6 x 6
  38. # Groups: Pclass, agecat [3]
  39. Pclass agecat Sex N survivors perc_survived
  40. <int> <fctr> <chr> <int> <int> <dbl>
  41. 1 1 Under 15 female 2 1 50.00000
  42. 2 1 Under 15 male 3 3 100.00000
  43. 3 1 15 to 50 female 70 68 97.14286
  44. 4 1 15 to 50 male 72 32 44.44444
  45. 5 1 Over 50 female 13 13 100.00000
  46. 6 1 Over 50 male 26 5 19.23077
  47.  
  48. ## Pclass agecat Sex N survivors perc_survived
  49. ## <int> <fctr> <chr> <int> <int> <dbl>
  50. ## 1 Under 15 female 2 1 50.000000
  51. ## 1 Under 15 male 3 3 100.000000
  52. ## 1 15 to 50 female 70 68 97.142857
  53. ## 1 15 to 50 male 72 32 44.444444
  54. ## 1 Over 50 female 13 13 100.000000
  55. ## 1 Over 50 male 26 5 19.230769
Add Comment
Please, Sign In to add comment