GSS extraction 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.0
✔ 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(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
survey <- read_excel("GSS_assignment.xlsx")
survey |> 
  mutate(race = as_factor(race)) |> 
  
  mutate(race = fct_recode(race,
                            NULL = ".i:  Inapplicable",
                            )) |> 

  drop_na(race) |> 
  
  mutate(race = fct_infreq(race)) |> 
  
  plot_ly(x = ~race) |> 
  add_histogram()
  1. This is a histogram what race individuals considered themselves in the years 2002 and 2022.
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. This is a histogram of what individuals believe about using spanking as a form of discipline for children from the years 2002 and 2022.
survey |> 
  
   mutate(race = as_factor(race)) |> 
  
  mutate(race = fct_recode(race,
                            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. This is a box plot of belief about using spanking as a form of discipline with children by race (1=strongly disagree, 2=disagree, 3=agree, 4=strongly agree).
survey |>
  
  mutate(Children = as.numeric(childs)) |>
  
  mutate(childs = fct_recode(childs,
                            NULL = ".i:  Inapplicable",
                            NULL = ".d:  Do not Know/Cannot Choose")) |>

  drop_na(childs) |>
  
  mutate(Children = fct_recode(childs,
                           "8" = "8 or more")) |> 
  
  mutate(childs = fct_relevel(childs,
                              "0",
                              "1",
                              "2",
                              "3",
                              "4",
                              "5",
                              "6",
                              "7",
                              "8")) |>
  
 
  
plot_ly(x = ~Children) |>
  add_histogram()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Children = as.numeric(childs)`.
Caused by warning:
! NAs introduced by coercion
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `childs = fct_relevel(childs, "0", "1", "2", "3", "4", "5", "6",
  "7", "8")`.
Caused by warning:
! 1 unknown level in `f`: 8
  1. This is a histogram showing the number of biological children respondents reported in 2002 and 2022.
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")) |> 
  
    mutate(spanking = fct_relevel(spanking,
                              c("STRONGLY DISAGREE",
                                "DISAGREE",
                                "AGREE",
                                "STRONGLY AGREE"))) |> 
 drop_na(spanking) |>
  
 mutate(Children = as.numeric(childs)) |>
  
  mutate(childs = fct_recode(childs,
                            NULL = ".i:  Inapplicable",
                            NULL = ".d:  Do not Know/Cannot Choose")) |>

  drop_na(childs) |>
  
  mutate(Children = fct_recode(childs,
                           "8" = "8 or more")) |> 
  
  mutate(childs = fct_relevel(childs,
                              "0",
                              "1",
                              "2",
                              "3",
                              "4",
                              "5",
                              "6",
                              "7",
                              "8 or more")) |>

 
  
plot_ly(x = ~Children, y = ~spanking) |>
add_boxplot()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Children = as.numeric(childs)`.
Caused by warning:
! NAs introduced by coercion
  1. This is a box plot showing belief about using spanking as a form of discipline based on how many biological children the individual has.
survey |> 
  mutate(hapmar = as_factor(hapmar)) |> 
  
  mutate(hapmar = fct_recode(hapmar,
                            NULL = ".i:  Inapplicable",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
  mutate(hapmar = fct_infreq(hapmar)) |> 
  
  plot_ly(x = ~hapmar) |> 
  add_histogram()
Warning: Ignoring 4251 observations
  1. This is a histogram for how happy individuals said they were within their marriages for the years 2002 and 2022.
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")) |> 

   mutate(class_ = fct_relevel(class_,
                              "Lower class",
                              "Working class",
                              "Middle class",
                              "Upper class")) |>
                             
 
  
  plot_ly(x = ~class_) |> 
  add_histogram()
Warning: Ignoring 55 observations
  1. This is a histogram for what class individuals identified themselves in for the years 2002 and 2022.
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")) |> 

   mutate(class_ = fct_relevel(class_,
                              "Lower class",
                              "Working class",
                              "Middle class",
                              "Upper class")) |>
                             
  mutate(hapmar = as_factor(hapmar)) |> 
  
  mutate(hapmar = fct_recode(hapmar,
                            NULL = ".i:  Inapplicable",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
   mutate(hapmar = fct_infreq(hapmar)) |>
  
  plot_ly(x = ~hapmar, color = ~class_) |> 
  add_histogram()
Warning: Ignoring 4251 observations
  1. This is a histogram showing the class of individual for each category of how happy they are within their marriage.
survey |>
  
 mutate(age = as_factor(age)) |> 
  
  mutate(age = fct_recode(age,
                            
                            NULL = ".n:  No answer")) |> 
                            
  
   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,
                            "30s" = c("30",
                                      "31",
                                      "32",
                                      "33",
                                      "34",
                                      "35",
                                      "36",
                                      "37",
                                      "38",
                                      "39"))) |>
   mutate(age = fct_collapse(age,
                            "40s" = c("40",
                                      "41",
                                      "42",
                                      "43",
                                      "44",
                                      "45",
                                      "46",
                                      "47",
                                      "48",
                                      "49"))) |>
   mutate(age = fct_collapse(age,
                            "50s" = c("50",
                                      "51",
                                      "52",
                                      "53",
                                      "54",
                                      "55",
                                      "56",
                                      "57",
                                      "58",
                                      "59"))) |>
   mutate(age = fct_collapse(age,
                            "60s" = c("60",
                                      "61",
                                      "62",
                                      "63",
                                      "64",
                                      "65",
                                      "66",
                                      "67",
                                      "68",
                                      "69"))) |>
   mutate(age = fct_collapse(age,
                            "70s" = c("70",
                                      "71",
                                      "72",
                                      "73",
                                      "74",
                                      "75",
                                      "76",
                                      "77",
                                      "78",
                                      "79"))) |>
   mutate(age = fct_collapse(age,
                            "80 and up" = c("80",
                                      "81",
                                      "82",
                                      "83",
                                      "84",
                                      "85",
                                      "86",
                                      "87",
                                      "88",
                                      "89 or older"))) |>
  plot_ly(x = ~age) |>
  add_histogram()
Warning: Ignoring 222 observations
  1. This is a histogram of the ages of individuals surveyed for years 2002 and 2022.
survey |>
  
  mutate(age = as_factor(age)) |> 
  
  mutate(age = fct_recode(age,
                            
                            NULL = ".n:  No answer")) |> 
                            
  
   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,
                            "30s" = c("30",
                                      "31",
                                      "32",
                                      "33",
                                      "34",
                                      "35",
                                      "36",
                                      "37",
                                      "38",
                                      "39"))) |>
   mutate(age = fct_collapse(age,
                            "40s" = c("40",
                                      "41",
                                      "42",
                                      "43",
                                      "44",
                                      "45",
                                      "46",
                                      "47",
                                      "48",
                                      "49"))) |>
   mutate(age = fct_collapse(age,
                            "50s" = c("50",
                                      "51",
                                      "52",
                                      "53",
                                      "54",
                                      "55",
                                      "56",
                                      "57",
                                      "58",
                                      "59"))) |>
   mutate(age = fct_collapse(age,
                            "60s" = c("60",
                                      "61",
                                      "62",
                                      "63",
                                      "64",
                                      "65",
                                      "66",
                                      "67",
                                      "68",
                                      "69"))) |>
   mutate(age = fct_collapse(age,
                            "70s" = c("70",
                                      "71",
                                      "72",
                                      "73",
                                      "74",
                                      "75",
                                      "76",
                                      "77",
                                      "78",
                                      "79"))) |>
   mutate(age = fct_collapse(age,
                            "80 and up" = c("80",
                                      "81",
                                      "82",
                                      "83",
                                      "84",
                                      "85",
                                      "86",
                                      "87",
                                      "88",
                                      "89 or older"))) |>
   mutate(hapmar = as_factor(hapmar)) |> 
  
  mutate(hapmar = fct_recode(hapmar,
                            NULL = ".i:  Inapplicable",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
  plot_ly(x = ~hapmar, y = ~age) |> 
  add_histogram2d()
  1. This is a heatmap showing the interaction between age and happiness within marriage for individuals surveyed in 2002 and 2022.