GSS Assignment submission

GSS Assignment

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── 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)                   
library(htmlwidgets)
library(devtools) 
Loading required package: usethis
library(plotly)

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
library(conflicted)
devtools::install_github("ropensci/plotly")
Skipping install of 'plotly' from a github remote, the SHA1 (16261c33) has not changed since last install.
  Use `force = TRUE` to force installation
survey <- read_excel("GSS.xlsx")
glimpse(survey)
Rows: 72,390
Columns: 16
$ year     <dbl> 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1972, 1…
$ 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", "5", "4", "0", "2", "0", "2", "0", "2", "4", "1", "5", "…
$ age      <chr> "23", "70", "48", "27", "61", "26", "28", "27", "21", "30", "…
$ sex      <chr> "FEMALE", "MALE", "FEMALE", "FEMALE", "FEMALE", "MALE", "MALE…
$ race     <chr> "White", "White", "White", "White", "White", "White", "White"…
$ courts   <chr> "About right", "Not harshly enough", "Not harshly enough", "A…
$ relig    <chr> "Jewish", "Catholic", "Protestant", "Other", "Protestant", "P…
$ attend   <chr> "About once or twice a year", "Every week", "About once a mon…
$ hapmar   <chr> ".i:  Inapplicable", ".i:  Inapplicable", ".i:  Inapplicable"…
$ class_   <chr> "Middle class", "Middle class", "Working class", "Middle clas…
$ premarsx <chr> "NOT WRONG AT ALL", "ALWAYS WRONG", "ALWAYS WRONG", "ALWAYS W…
$ xmarsex  <chr> ".i:  Inapplicable", ".i:  Inapplicable", ".i:  Inapplicable"…
$ spanking <chr> ".i:  Inapplicable", ".i:  Inapplicable", ".i:  Inapplicable"…
$ ballot   <chr> ".i:  Inapplicable", ".i:  Inapplicable", ".i:  Inapplicable"…
survey |> 
  mutate(sex = as_factor(sex)) |> 
  
  mutate(sex = fct_recode(sex,
                          "Female" = "FEMALE",
                          "Male" = "MALE",
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:  No answer",))
# A tibble: 72,390 × 16
    year   id_ hrs2   childs age   sex   race  courts relig attend hapmar class_
   <dbl> <dbl> <chr>  <chr>  <chr> <fct> <chr> <chr>  <chr> <chr>  <chr>  <chr> 
 1  1972     1 .i:  … 0      23    Fema… White About… Jewi… About… .i:  … Middl…
 2  1972     2 .i:  … 5      70    Male  White Not h… Cath… Every… .i:  … Middl…
 3  1972     3 .i:  … 4      48    Fema… White Not h… Prot… About… .i:  … Worki…
 4  1972     4 .i:  … 0      27    Fema… White About… Other Never  .i:  … Middl…
 5  1972     5 .i:  … 2      61    Fema… White Not h… Prot… Never  .i:  … Worki…
 6  1972     6 .i:  … 0      26    Male  White .d:  … Prot… About… .i:  … Middl…
 7  1972     7 .i:  … 2      28    Male  White Not h… Cath… Every… .i:  … Middl…
 8  1972     8 .i:  … 0      27    Male  White .d:  … Jewi… Never  .i:  … Worki…
 9  1972     9 .i:  … 2      21    Fema… Black Not h… Prot… Sever… .i:  … Worki…
10  1972    10 .i:  … 4      30    Fema… Black Not h… Prot… Sever… .i:  … Worki…
# ℹ 72,380 more rows
# ℹ 4 more variables: premarsx <chr>, xmarsex <chr>, spanking <chr>,
#   ballot <chr>
survey |> 
  mutate(race = as_factor(race)) |>
  mutate(race = fct_recode(race,
                            NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:   No answer", 
                          NULL = ".i:  Inapplicable")) |>
    
  drop_na(race) |> 

  plot_ly(x = ~race) |> 
  add_histogram()
survey |> 
  mutate(spanking = as_factor(spanking)) |>
  mutate(spanking = fct_recode(spanking,
                               "Strongly Disagree" = "STRONGLY DISAGREE",
                               "Disagree" = "DISAGREE",
                               "Agree" = "AGREE",
                               "Strongly Agree" = "STRONGLY AGREE",
                            NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:  No answer",
                          NULL = ".i:  Inapplicable")) |>
  mutate(spanking = fct_relevel(spanking,
                              c("Strongly Disagree",
                                "Disagree",
                                "Agree",
                                "Strongly Agree"))) |> 
  drop_na(spanking) |> 
  
  plot_ly(x = ~spanking) |> 
  add_histogram()
survey |> 
  mutate(race = as_factor(race)) |>
  mutate(race = fct_recode(race,
                            NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:  No answer",
                          NULL = ".i:  Inapplicable")) |>
    
  drop_na(race) |> 
 mutate(race = fct_infreq(race)) |>

  mutate(spanking = as_factor(spanking)) |>
  mutate(spanking = fct_recode(spanking,
                               "Strongly Disagree" = "STRONGLY DISAGREE",
                               "Disagree" = "DISAGREE",
                               "Agree" = "AGREE",
                               "Strongly Agree" = "STRONGLY AGREE",
                            NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:  No answer",
                          NULL = ".i:  Inapplicable")) |>
  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()
  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")) |> 
  
    
  drop_na(childs) |> 
  
  mutate(childs = as.numeric(childs)) |> 

  plot_ly(x = ~childs) |> 
  add_histogram()
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")) |> 
  
    
  drop_na(childs) |> 
  
  mutate(childs = as.numeric(childs)) |> 

  mutate(spanking = as_factor(spanking)) |>
  mutate(spanking = fct_recode(spanking,
                               "Strongly Disagree" = "STRONGLY DISAGREE",
                               "Disagree" = "DISAGREE",
                               "Agree" = "AGREE",
                               "Strongly Agree" = "STRONGLY AGREE",
                            NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:  No answer",
                          NULL = ".i:  Inapplicable")) |>
  mutate(spanking = fct_relevel(spanking,
                              c("Strongly Disagree",
                                "Disagree",
                                "Agree",
                                "Strongly Agree",
                                "Inapplicable"))) |> 
  drop_na(spanking) |> 
  

plot_ly(x = ~spanking, y = ~childs) |> 
  add_boxplot()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `spanking = fct_relevel(...)`.
Caused by warning:
! 1 unknown level in `f`: Inapplicable

6.

survey |> 
    mutate(hapmar = as_factor(hapmar)) |> 
  
    mutate(hapmar = fct_recode(hapmar,
                               "Not Too Happy" = "NOT TOO HAPPY",
                               "Pretty Happy" = "PRETTY HAPPY",
                               "Very Happy" = "VERY HAPPY",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
 
  drop_na(hapmar) |>
  mutate(hapmar = fct_infreq(hapmar)) |>
  
  plot_ly(x = ~hapmar) |> 
  add_histogram()
survey |> 
    mutate(class_ = as_factor(class_)) |> 
  
    mutate(class_ = fct_recode(class_,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = "No class")) |>
  
  mutate(class_ = fct_relevel(class_,
                              c("Lower class",
                                "Working class",
                                "Middle class",
                                "Upper class"))) |>
 
  drop_na(class_) |>
  
  plot_ly(x = ~class_) |> 
  add_histogram()
survey |>
    mutate(hapmar = as_factor(hapmar)) |> 
  
    mutate(hapmar = fct_recode(hapmar,
                               "Not Too Happy" = "NOT TOO HAPPY",
                               "Pretty Happy" = "PRETTY HAPPY",
                               "Very Happy" = "VERY HAPPY",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
   mutate(hapmar = fct_relevel(hapmar,
                              c("Not too Happy",
                                "Pretty Happy",
                                "Very Happy"))) |>
 
  drop_na(hapmar) |>
  
    mutate(class_ = as_factor(class_)) |> 
  
    mutate(class_ = fct_recode(class_,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                            NULL = "No class")) |>
  
  mutate(class_ = fct_relevel(class_,
                              c("Lower class",
                                "Working class",
                                "Middle class",
                                "Upper class"))) |>
 
  drop_na(class_) |>
  
  plot_ly(x = ~class_, color = ~hapmar) |> 
  add_histogram()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `hapmar = fct_relevel(hapmar, c("Not too Happy", "Pretty Happy",
  "Very Happy"))`.
Caused by warning:
! 1 unknown level in `f`: Not too Happy
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",
                          NULL = ".i:  Inapplicable",
                          NULL = "89 or older")) |> 
  
   mutate(age = fct_collapse(age,
                              "under 30" = c("18",
                                          "19",
                                          "20",
                                          "21",
                                          "22",
                                          "23",
                                          "24",
                                          "25",
                                          "26",
                                          "27",
                                          "28",
                                          "29"),
                             "30's" = c("30",
                                        "31",
                                        "32",
                                        "33",
                                        "34",
                                        "35",
                                        "36",
                                        "37",
                                        "38",
                                        "39"),
                             "40's" = c("40",
                                        "41",
                                        "42",
                                        "43",
                                        "44",
                                        "45",
                                        "46",
                                        "47",
                                        "48",
                                        "49"),
                            "50's" = c("50",
                                       "51",
                                       "52",
                                       "53",
                                       "54",
                                       "55",
                                       "56",
                                       "57",
                                       "58",
                                       "59"),
                            "60's" = c("60",
                                       "61",
                                       "62",
                                       "63",
                                       "64",
                                       "65",
                                       "66",
                                       "67",
                                       "68",
                                       "69"),
                            "70's" = c("70",
                                       "71",
                                       "72",
                                       "73",
                                       "74",
                                       "75",
                                       "76",
                                       "77",
                                       "78",
                                       "79"),
                            "80 and up" = 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 and up"))) |>
  drop_na(age) |> 

  
  plot_ly(x = ~age) |> 
  add_histogram()

10.

survey |> 
    mutate(hapmar = as_factor(hapmar)) |> 
  
    mutate(hapmar = fct_recode(hapmar,
                               "Not Too Happy" = "NOT TOO HAPPY",
                               "Pretty Happy" = "PRETTY HAPPY",
                               "Very Happy" = "VERY HAPPY",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            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 = ".n:  No answer",
                            NULL = ".s:  Skipped on Web",
                          NULL = ".i:  Inapplicable",
                          NULL = "89 or older")) |> 
  
   mutate(age = fct_collapse(age,
                              "under 30" = c("18",
                                          "19",
                                          "20",
                                          "21",
                                          "22",
                                          "23",
                                          "24",
                                          "25",
                                          "26",
                                          "27",
                                          "28",
                                          "29"),
                             "30's" = c("30",
                                        "31",
                                        "32",
                                        "33",
                                        "34",
                                        "35",
                                        "36",
                                        "37",
                                        "38",
                                        "39"),
                             "40's" = c("40",
                                        "41",
                                        "42",
                                        "43",
                                        "44",
                                        "45",
                                        "46",
                                        "47",
                                        "48",
                                        "49"),
                            "50's" = c("50",
                                       "51",
                                       "52",
                                       "53",
                                       "54",
                                       "55",
                                       "56",
                                       "57",
                                       "58",
                                       "59"),
                            "60's" = c("60",
                                       "61",
                                       "62",
                                       "63",
                                       "64",
                                       "65",
                                       "66",
                                       "67",
                                       "68",
                                       "69"),
                            "70's" = c("70",
                                       "71",
                                       "72",
                                       "73",
                                       "74",
                                       "75",
                                       "76",
                                       "77",
                                       "78",
                                       "79"),
                            "80 and up" = 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 and up"))) |>
  drop_na(age) |> 
  
  mutate(age = fct_infreq(age)) |> 
  
  plot_ly(x = ~hapmar, y = ~age) |> 
  add_histogram2d()