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)                   # package to open excel files
Warning: package 'readxl' was built under R version 4.2.3
library(plotly)                   # a graphics package, and alternative to ggplot2 
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("GSS_assignment.xlsx.xlsx")

Make sure you rename the file you downloaded from the GSS data explorer site, and you put it into your class directory/folder. Otherwise the following will not work.

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")) |> 

  drop_na(race) |> 
  
  mutate(race = fct_infreq(race)) |> 
  
  plot_ly(x = ~race) |> 
  add_histogram()
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")) |> 

  drop_na(spanking) |> 
  
  mutate(spanking = fct_infreq(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 = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  
  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")) |> 
  
  
  drop_na(spanking) |> 
  
  mutate(spanking = as.numeric(spanking)) |> 
  

plot_ly(x = ~race, y = ~spanking) |> 
  add_boxplot()
survey |> 
  mutate(childs = as_factor(childs)) |> 
  
  mutate(relig = fct_recode(childs,
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 

  drop_na(childs) |> 
  
  mutate(childs = fct_infreq(childs)) |> 
  
  drop_na(childs) |> 
  
  mutate(childs = as.numeric(childs)) |> 
  
  
  plot_ly(x = ~childs) |> 
  add_histogram()
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")) |> 
  
  drop_na(spanking) |> 
  
  mutate(spanking = fct_infreq(spanking)) |> 
  
  
  mutate(childs = as_factor(childs)) |> 
    mutate(spanking = 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()
Warning: Ignoring 14 observations
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")) |> 

  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 = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) |> 
  

  drop_na(class_) |> 
  
  mutate(class_ = fct_infreq(class_)) |> 
  
  plot_ly(x = ~class_) |> 
  add_histogram()
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")) |> 
  
  
  drop_na(class_) |> 
  
  mutate(class_ = fct_infreq(class_)) |> 
  
  mutate(hapmar = as_factor(hapmar)) |> 
  
  mutate(hapmar = fct_recode(hapmar,
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".s:  Skipped on Web",
                          NULL = ".n:  No answer")) |> 
  
  drop_na(hapmar) |> 

  plot_ly(x = ~class_, color = ~hapmar) |> 
  add_histogram()
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,
                              "20s" = c("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" = c("84, 81, 82, 85, 87"))) |> 

   drop_na(age) |> 
  
   count(age)|>
  
   mutate(age = fct_infreq(age)) |> 
  
   plot_ly(x = ~age) |> 
   add_histogram()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(age, `20s` =
  c("20,21,22,23,24,25,26,27,28,29"))`.
Caused by warning:
! Unknown levels in `f`: 20,21,22,23,24,25,26,27,28,29
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(age, `30s` =
  c("30,31,32,33,34,35,36,37,38,39"))`.
Caused by warning:
! Unknown levels in `f`: 30,31,32,33,34,35,36,37,38,39
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(age, `40s` =
  c("40,41,42,43,44,45,46,47,48,49"))`.
Caused by warning:
! Unknown levels in `f`: 40,41,42,43,44,45,46,47,48,49
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(age, `50s` =
  c("50,51,52,53,54,55,56,57,58,59"))`.
Caused by warning:
! Unknown levels in `f`: 50,51,52,53,54,55,56,57,58,59
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(age, `60s` =
  c("60,61,62,63,64,65,66,67,68,69"))`.
Caused by warning:
! Unknown levels in `f`: 60,61,62,63,64,65,66,67,68,69
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(age, `70s` =
  c("70,71,72,73,74,75,76,77,78,79"))`.
Caused by warning:
! Unknown levels in `f`: 70,71,72,73,74,75,76,77,78,79
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(age, `80` = c("84, 81, 82, 85, 87"))`.
Caused by warning:
! Unknown levels in `f`: 84, 81, 82, 85, 87