GSS Assignment

Author

Kendall W.

GSS Assignment

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.2     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ ggplot2   3.4.2     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.1     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)                   # package to open excel files
library(plotly)                   # a graphics package, and alternative to ggplot2 

Attaching package: 'plotly'

The following object is masked from 'package:ggplot2':

    last_plot

The following object is masked from 'package:stats':

    filter

The following object is masked from 'package:graphics':

    layout
survey <- read_excel("GSS_assignment.xlsx")
glimpse(survey)
Rows: 6,309
Columns: 15
$ year     <dbl> 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2002, 2…
$ id_      <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18…
$ hrs2     <chr> ".i:  Inapplicable", ".i:  Inapplicable", ".i:  Inapplicable"…
$ childs   <chr> "0", "1", "1", "1", "2", "1", "2", "2", "2", "0", "2", "3", "…
$ age      <chr> "25", "43", "30", "55", "37", "47", "57", "71", "46", "19", "…
$ sex      <chr> "FEMALE", "MALE", "FEMALE", "FEMALE", "MALE", "MALE", "FEMALE…
$ race     <chr> "White", "White", "White", "White", "White", "White", "White"…
$ courts   <chr> "About right", "Not harshly enough", ".i:  Inapplicable", ".i…
$ relig    <chr> "Inter-nondenominational", "Protestant", "Protestant", "Prote…
$ attend   <chr> "About once or twice a year", "About once a month", "Every we…
$ hapmar   <chr> ".i:  Inapplicable", "PRETTY HAPPY", ".i:  Inapplicable", ".i…
$ class_   <chr> "Middle class", "Middle class", "Working class", "Upper class…
$ premarsx <chr> "ALWAYS WRONG", ".i:  Inapplicable", ".i:  Inapplicable", ".i…
$ xmarsex  <chr> "ALWAYS WRONG", "ALWAYS WRONG", ".i:  Inapplicable", ".i:  In…
$ spanking <chr> "STRONGLY AGREE", ".i:  Inapplicable", ".i:  Inapplicable", "…
survey |> 
  count(race)
# A tibble: 4 × 2
  race                  n
  <chr>             <int>
1 .i:  Inapplicable    53
2 Black               975
3 Other               579
4 White              4702
survey |> 
  mutate(race = as_factor(race)) |> 
  
  mutate(race = fct_recode(race,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".i:  Inapplicable")) |>

  drop_na(race) |> 
  
  mutate(race = fct_infreq(race)) |> 
  
  plot_ly(x = ~race) |> 
  add_histogram()
  1. This histogram shows the majority of people who participated in the survey were white with 4,702 white respondents. The second most was black with 975 respondents. And lastly was other with 579 respondents.
survey |> 
  mutate(spanking = as_factor(spanking)) |> 
  
  mutate(spanking = fct_recode(spanking,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".i:  Inapplicable",
                            NULL = ".s:  Skipped on Web")) |> 

  drop_na(spanking) |> 
  
  mutate(spanking = fct_infreq(spanking)) |> 
  
  plot_ly(x = ~spanking) |> 
  add_histogram()
  1. The histogram shows peoples stances on spankings. Most people agreed with spanking with 1,274 respondents answering agree. While the least amount of people responded with strongly disagree with 453 responses.
survey |> 
  count(spanking)
# A tibble: 8 × 2
  spanking                           n
  <chr>                          <int>
1 .d:  Do not Know/Cannot Choose    29
2 .i:  Inapplicable               3025
3 .n:  No answer                     5
4 .s:  Skipped on Web                7
5 AGREE                           1274
6 DISAGREE                         810
7 STRONGLY AGREE                   706
8 STRONGLY DISAGREE                453
survey |> 
    mutate(spanking = as_factor(spanking)) |> 
  
    mutate(spanking = fct_recode(spanking,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |> 
  
    mutate(spanking = fct_relevel(spanking,
                              c("STRONGLY DISAGREE",
                                "DISAGREE",
                                "AGREE",
                                "STRONGLY AGREE"))) |> 
  drop_na(spanking) |> 
  
  mutate(spanking = as.numeric(spanking)) |> 
  
  count(spanking)
# A tibble: 4 × 2
  spanking     n
     <dbl> <int>
1        1   453
2        2   810
3        3  1274
4        4   706
survey |> 
  
   mutate(race = as_factor(race)) |> 
  
  mutate(race = fct_recode(race,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |> 
  drop_na(race) |> 
  
  mutate(race = fct_infreq(race)) |> 
  
  
  mutate(spanking = as_factor(spanking)) |> 
    mutate(spanking = fct_recode(spanking,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
    mutate(spanking = fct_relevel(spanking,
                              c("STRONGLY DISAGREE",
                                "DISAGREE",
                                "AGREE",
                                "STRONGLY AGREE"))) |> 
  drop_na(spanking) |> 
  
  mutate(spanking = as.numeric(spanking)) |> 
  

plot_ly(x = ~race, y = ~spanking) |> 
  add_boxplot()
  1. The box plot shows that all the races had similar views on spanking as they all had a median of 3.
survey |> 
  count(childs)
# A tibble: 11 × 2
   childs                             n
   <chr>                          <int>
 1 .d:  Do not Know/Cannot Choose     5
 2 .i:  Inapplicable                 11
 3 0                               1906
 4 1                               1014
 5 2                               1571
 6 3                               1000
 7 4                                430
 8 5                                169
 9 6                                 81
10 7                                 47
11 8 or more                         75
survey |> 
    mutate(childs = as_factor(childs)) |> 
  
    mutate(childs = fct_recode(childs,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |> 
  drop_na(childs) |> 
  
  mutate(childs = as.numeric(childs)) |> 
  
  count(childs)
# A tibble: 9 × 2
  childs     n
   <dbl> <int>
1      1  1906
2      2  1014
3      3  1571
4      4  1000
5      5    81
6      6   430
7      7    75
8      8   169
9      9    47
survey |> 
  mutate(childs = as_factor(childs)) |> 
  
  mutate(childs = fct_recode(childs,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |> 
  mutate(childs = fct_recode(childs,
                          "8" = "8 or more")) |>
  drop_na(childs) |> 
  
  mutate(childs = fct_infreq(childs)) |> 
  
  plot_ly(x = ~childs) |> 
  add_histogram()
  1. This histogram shows that most children fall into 0, with 1906.
survey |> 
  
   mutate(spanking = as_factor(spanking)) |> 
  
  mutate(spanking = fct_recode(spanking,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |> 
  drop_na(spanking) |> 
  
  mutate(spanking = fct_infreq(spanking)) |> 
  
      mutate(spanking = fct_relevel(spanking,
                              c("STRONGLY DISAGREE",
                                "DISAGREE",
                                "AGREE",
                                "STRONGLY AGREE"))) |> 
  
  
  mutate(childs = as_factor(childs)) |> 
    mutate(childs = fct_recode(childs,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
  drop_na(childs) |> 
  
  mutate(childs = as.numeric(childs)) |> 
  

plot_ly(x = ~spanking, y = ~childs) |> 
  add_boxplot()
  1. This box plot shows that strongly disagree had a median of 2 as the lowest. Disagree, Agree, and Strongly Agree were equal with medians of 3.
survey |> 
  count(hapmar)
# A tibble: 7 × 2
  hapmar                             n
  <chr>                          <int>
1 .d:  Do not Know/Cannot Choose     2
2 .i:  Inapplicable               4234
3 .n:  No answer                    12
4 .s:  Skipped on Web                3
5 NOT TOO HAPPY                     90
6 PRETTY HAPPY                     717
7 VERY HAPPY                      1251
survey |> 
  mutate(hapmar = as_factor(hapmar)) |> 
  
  mutate(hapmar = fct_recode(hapmar,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |>
  
  drop_na(hapmar) |> 
  
  mutate(hapmar = fct_infreq(hapmar)) |> 
  
  plot_ly(x = ~hapmar) |> 
  add_histogram()
  1. The histogram shows that the majority have happy marriages with 1,251 responses, pretty happy had 717, and 90 were not too happy in their marriage.
survey |> 
  count(class_)
# A tibble: 7 × 2
  class_                             n
  <chr>                          <int>
1 .d:  Do not Know/Cannot Choose    36
2 .n:  No answer                    12
3 .s:  Skipped on Web                7
4 Lower class                      547
5 Middle class                    2749
6 Upper class                      256
7 Working class                   2702
survey |> 
  mutate(class_ = as_factor(class_)) |> 
  
  mutate(class_ = fct_recode(class_,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |>
  
  mutate(class_ = fct_relevel(class_,
                              c("Lower class",
                                "Working class",
                                "Middle class",
                                "Upper class"))) |>
  drop_na(class_) |> 
  
 
  
  plot_ly(x = ~class_) |> 
  add_histogram()
  1. Histogram shows that most respondents were in the working and middle class, while the least were in upper and lower class.
survey |> 
  mutate(hapmar = as_factor(hapmar)) |> 
  
  mutate(hapmar = fct_recode(hapmar,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |> 
  

  drop_na(hapmar) |> 
  
  mutate(hapmar = fct_infreq(hapmar)) |> 
  
  mutate(class_ = as_factor(class_)) |> 
  
  mutate(class_ = fct_recode(class_,
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:  No answer")) |> 
   mutate(class_ = fct_relevel(class_,
                              c("Lower class",
                                "Working class",
                                "Middle class",
                                "Upper class"))) |>
  
  drop_na(class_) |> 

  plot_ly(x = ~hapmar, color = ~class_) |> 
  add_histogram()
  1. Histogram shows how the different classes are feeling about their marriages.
survey |> 
  count(age)
# A tibble: 73 × 2
   age                n
   <chr>          <int>
 1 .n:  No answer   222
 2 18                26
 3 19                54
 4 20                80
 5 21                86
 6 22                92
 7 23               121
 8 24               103
 9 25               106
10 26               112
# ℹ 63 more rows
survey |> 
  mutate(age = as_factor(age)) |> 
  
  mutate(age = fct_recode(age,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
   mutate(age = fct_collapse(age,
                              "Under 30" = c("18",
                                          "19",
                                          "20",
                                          "21",
                                          "22",
                                          "23",
                                          "24",
                                          "25",
                                          "26",
                                          "27", "28", "29"))) |> 
  mutate(age = fct_collapse(age, "30's" = c("30",
                                          "31",
                                          "32",
                                          "33",
                                          "34",
                                          "35",
                                          "36",
                                          "37",
                                          "38",
                                          "39"))) |>
  mutate(age = fct_collapse(age, "40's" = c("40",
                                          "41",
                                          "42",
                                          "43",
                                          "44",
                                          "45",
                                          "46",
                                          "47",
                                          "48",
                                          "49"))) |>
  mutate(age = fct_collapse(age, "50's" = c("50",
                                          "51",
                                          "52",
                                          "53",
                                          "54",
                                          "55",
                                          "56",
                                          "57",
                                          "58",
                                          "59"))) |>
  mutate(age = fct_collapse(age, "60's" = c("60",
                                          "61",
                                          "62",
                                          "63",
                                          "64",
                                          "65",
                                          "66",
                                          "67",
                                          "68",
                                          "69"))) |>
  mutate(age = fct_collapse(age, "70's" = c("70",
                                          "71",
                                          "72",
                                          "73",
                                          "74",
                                          "75",
                                          "76",
                                          "77",
                                          "78",
                                          "79"))) |>
  mutate(age = fct_collapse(age, "80's" = c("80",
                                          "81",
                                          "82",
                                          "83",
                                          "84",
                                          "85",
                                          "86",
                                          "87",
                                          "88"))) |>
  drop_na(age) |> 
  
  mutate(age = fct_infreq(age)) |> 
  
  plot_ly(x = ~age) |> 
  add_histogram()
  1. Histogram shows that most respondents were in their 30’s and the second most responses were from individuals under 30.
survey |> 
  
   mutate(hapmar = as_factor(hapmar)) |> 
  
  mutate(hapmar = fct_recode(hapmar,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".i:  Inapplicable",
                            NULL = ".s:  Skipped on Web")) |> 
  
  drop_na(hapmar) |> 
  
  mutate(hapmar = fct_infreq(hapmar)) |> 
  
  
  mutate(age = as_factor(age)) |> 
    mutate(age = fct_recode(age,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
     mutate(age = fct_collapse(age,
                              "Under 30" = c("18",
                                          "19",
                                          "20",
                                          "21",
                                          "22",
                                          "23",
                                          "24",
                                          "25",
                                          "26",
                                          "27", "28", "29"))) |> 
  mutate(age = fct_collapse(age, "30's" = c("30",
                                          "31",
                                          "32",
                                          "33",
                                          "34",
                                          "35",
                                          "36",
                                          "37",
                                          "38",
                                          "39"))) |>
  mutate(age = fct_collapse(age, "40's" = c("40",
                                          "41",
                                          "42",
                                          "43",
                                          "44",
                                          "45",
                                          "46",
                                          "47",
                                          "48",
                                          "49"))) |>
  mutate(age = fct_collapse(age, "50's" = c("50",
                                          "51",
                                          "52",
                                          "53",
                                          "54",
                                          "55",
                                          "56",
                                          "57",
                                          "58",
                                          "59"))) |>
  mutate(age = fct_collapse(age, "60's" = c("60",
                                          "61",
                                          "62",
                                          "63",
                                          "64",
                                          "65",
                                          "66",
                                          "67",
                                          "68",
                                          "69"))) |>
  mutate(age = fct_collapse(age, "70's" = c("70",
                                          "71",
                                          "72",
                                          "73",
                                          "74",
                                          "75",
                                          "76",
                                          "77",
                                          "78",
                                          "79"))) |>
  mutate(age = fct_collapse(age, "80's" = c("80",
                                          "81",
                                          "82",
                                          "83",
                                          "84",
                                          "85",
                                          "86",
                                          "87",
                                          "88"))) |>
   mutate(age = fct_relevel(age,
                              c("Under 30",
                                "30's",
                                "40's",
                                "50's",
                                "60's",
                                "70's",
                                "80's",
                                "89 or older"))) |>
  

  drop_na(age) |> 
  

  plot_ly(x = ~hapmar, y = ~age) |> 
  add_histogram2d()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(...)`.
Caused by warning:
! Unknown levels in `f`: 18, 19
  1. Heat map shows that most of the happy marriages were in the 30’s and 40’s.