GSS Assignment

library(tidyverse)
Warning: package 'tidyverse' was built under R version 4.2.3
Warning: package 'ggplot2' was built under R version 4.2.3
Warning: package 'tibble' was built under R version 4.2.3
Warning: package 'tidyr' was built under R version 4.2.3
Warning: package 'readr' was built under R version 4.2.3
Warning: package 'purrr' was built under R version 4.2.3
Warning: package 'dplyr' was built under R version 4.2.3
Warning: package 'stringr' was built under R version 4.2.3
Warning: package 'forcats' was built under R version 4.2.3
Warning: package 'lubridate' was built under R version 4.2.3
── 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)
Warning: package 'readxl' was built under R version 4.2.3
library(plotly)
Warning: package 'plotly' was built under R version 4.2.3

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("C:/Users/katri/Downloads/R class/GSS.xlsx")
glimpse(survey)
Rows: 6,309
Columns: 16
$ 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", "…
$ ballot   <chr> "Ballot a", "Ballot c", "Ballot a", "Ballot b", "Ballot c", "…
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",
                            NULL = ".s:  Skipped on Web"))|> 

  drop_na(race) |> 
  
  #mutate(race = fct_infreq(race)) |> 
  #count(race)
  plot_ly(x = ~race) |> 
  add_histogram()
  1. Histogram for race 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(race = fct_infreq(race)) |> 
  #count(race)
  plot_ly(x = ~spanking) |> 
  add_histogram()
  1. Histogram for how participants felt about spanking, most people agreed with spanking as a form of discipline 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 = ".s:  Skipped on Web",
                            NULL = ".i:  Inapplicable")) |> 
  
   mutate(spanking = fct_collapse(spanking, 
                                        "Other" =  c("STRONGLY AGREE",
                                          "AGREE",
                                          "DISAGREE",
                                          "STRONGLY DISAGREE"))) |> 
  
  drop_na(spanking) |> 
  
  mutate(spanking = fct_infreq(spanking)) |> 
  #mutate(spanking = as.numeric(spanking)) |> 
  
  
  
  mutate(race = as_factor(race)) |> 
    mutate(race = fct_recode(race,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
    mutate(race = fct_relevel(race,
                              c("Black",
                                "Other",
                                "White"))) |> 
  drop_na(race) |> 
  
  #mutate(race = as.numeric(race)) |> 
  

plot_ly(x = ~spanking, y = ~race) |> 
  add_boxplot()
survey |> 
  
  mutate(children = as.numeric(childs)) |>
  #mutate(childs = as_factor(childs)) |> 
  
  mutate(childs = fct_recode(childs,
                             "8" = "8 or more",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable")) |> 
  
  
   mutate(childs = fct_relevel(childs,
                              c("0",
                                "1",
                                "2",
                                "3",
                                "4",
                                "5",
                                "6",
                                "7",
                                "8"))) |> 
  
  drop_na(childs) |> 
  
  mutate(childs = fct_infreq(childs)) |> 
  
  mutate(childs = as.numeric(childs)) |>
  
  plot_ly(x = ~childs) |> 
  add_histogram()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `children = as.numeric(childs)`.
Caused by warning:
! NAs introduced by coercion
  1. Number of biological children that participants have.
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")) |> 
  
   mutate(spanking = fct_relevel(spanking,
                  c("STRONGLY AGREE",
                    "AGREE",
                    "DISAGREE",
                    "STRONGLY DISAGREE"))) |> 
  
  drop_na(spanking) |> 
  
  #mutate(spanking = fct_infreq(spanking)) |> 
  #mutate(spanking = as.numeric(spanking)) |> 
  
  #survey |> 
  
  mutate(children = as.numeric(childs)) |>
  
  #mutate(childs = as_factor(childs)) |> 
  
  mutate(childs = fct_recode(childs,
                             "8" = "8 or more",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable")) |> 
  
  
   mutate(childs = fct_relevel(childs,
                              c("0",
                                "1",
                                "2",
                                "3",
                                "4",
                                "5",
                                "6",
                                "7",
                                "8"))) |> 
  
  drop_na(childs) |> 
  
  #mutate(childs = fct_infreq(childs)) |> 
  
  
  plot_ly(x = ~spanking, y = ~children) |> 
  add_boxplot()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `children = as.numeric(childs)`.
Caused by warning:
! NAs introduced by coercion
Warning: Ignoring 44 observations
  1. Box plot showing the relationship between the number of children participants has and their beliefs on spanking.
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"))|> 
  
  mutate(hapmar = fct_relevel(hapmar,
                              c("NOT TOO HAPPY",
                                "PRETTY HAPPY",
                                "VERY HAPPY"))) |> 

  drop_na(hapmar) |> 
  
  
  
  #mutate(race = fct_infreq(race)) |> 

  plot_ly(x = ~hapmar) |> 
  add_histogram()
  1. Histogram showing how happy people were in their marriages (for the years of 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_,
                              c("Lower class",
                                "Working class",
                                "Middle class",
                                "Upper class"))) |> 

  drop_na(class_) |> 
  

  plot_ly(x = ~class) |> 
  add_histogram()
  1. Histogram displaying which class participants mostly belonged to in the years 2002 and 2022 (as you can see most were middle and working class).
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_,
                              c("Lower class",
                                "Working class",
                                "Middle class",
                                "Upper class"))) |> 

  drop_na(class_) |> 
  
  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"))|> 
  
  mutate(hapmar = fct_relevel(hapmar,
                              c("NOT TOO HAPPY",
                                "PRETTY HAPPY",
                                "VERY HAPPY"))) |> 

  drop_na(hapmar) |> 
  
  

 plot_ly(x = ~hapmar, color = ~class) |> 
  add_histogram()
  1. Histogram putting each of the classes in their own color and showing their marital status.
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. Histogram showing the age groups of the participants surveyed.
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 = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".i:  Inapplicable",
                            NULL = ".s:  Skipped on Web"))|> 
  
  mutate(hapmar = fct_relevel(hapmar,
                              c("NOT TOO HAPPY",
                                "PRETTY HAPPY",
                                "VERY HAPPY"))) |> 

  drop_na(hapmar) |> 
  
   plot_ly(x = ~hapmar, y = ~age) |> 
  add_histogram2d()
  1. Heatmap showing the interaction between marriage satisfaction and age.