Untitled

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)                   # package to open excel files
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("Desktop/Stats class/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. It looks like majority of data was gathered from white people.
survey %>% 
  mutate(spanking = as_factor(spanking)) %>% 
  
   mutate(spanking = fct_recode(spanking,
                               "Agree" = "AGREE",
                               "Disagree" = "DISAGREE",
                               "Strongly Disagree" = "STRONGLY DISAGREE",
                               "Strongly Agree" = "STRONGLY AGREE",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) %>% 
  drop_na(spanking) %>% 
    mutate(spanking = fct_infreq(spanking)) |> 
  
  plot_ly(x = ~spanking) |> 
  add_histogram()
  1. It looks like the majority of participants agree with spanking.
survey |> 
  
 mutate(race = as_factor(race)) %>% 
   mutate(race = fct_recode(race,
                         NULL = ".i:  Inapplicable")) |> 
  drop_na(race) %>%
  
  mutate(spanking = as_factor(spanking)) |> 
   mutate(spanking = fct_recode(spanking,
                               "Agree" = "AGREE",
                               "Disagree" = "DISAGREE",
                               "Strongly Disagree" = "STRONGLY DISAGREE",
                               "Strongly Agree" = "STRONGLY AGREE",
                            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 = ~spanking, y = ~race) |> 
  add_boxplot()
  1. It looks like the majority of people agree with spanking, regardless of race.
survey %>% 
  mutate(childs = as_factor(childs)) %>% 
  mutate(childs = fct_recode(childs, 
                             "8 or more " = "8",
                             NULL = ".i:  Inapplicable",
                              NULL = ".d:  Do not Know/Cannot Choose")) %>% 
  
  drop_na(childs) %>%
   mutate(childs = as.numeric(childs)) %>% 
      plot_ly(x = ~childs) |> 
  add_histogram()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `childs = fct_recode(...)`.
Caused by warning:
! Unknown levels in `f`: 8
  1. It looks like the majority of those surveyed had one child. 3 children was the next most common number of children between participants.
survey |> 
  
  mutate(childs = as_factor(childs)) %>% 
  mutate(childs = fct_recode(childs, 
                             "8 or more " = "8",
                             NULL = ".i:  Inapplicable",
                              NULL = ".d:  Do not Know/Cannot Choose")) %>% 
  drop_na(childs) %>%
   mutate(childs = as.numeric(childs)) %>% 
  
  mutate(spanking = as_factor(spanking)) |> 
   mutate(spanking = fct_recode(spanking,
                               "Agree" = "AGREE",
                               "Disagree" = "DISAGREE",
                               "Strongly Disagree" = "STRONGLY DISAGREE",
                               "Strongly Agree" = "STRONGLY AGREE",
                            NULL = ".d:  Do not Know/Cannot Choose",
                            NULL = ".i:  Inapplicable",
                            NULL = ".n:  No answer",
                            NULL = ".s:  Skipped on Web")) %>% 
  drop_na(spanking) %>% 
    mutate(spanking = fct_infreq(spanking)) |>
plot_ly(x = ~spanking, y = ~childs) |> 
 add_boxplot()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `childs = fct_recode(...)`.
Caused by warning:
! Unknown levels in `f`: 8
  1. This boxplot seems to show that those with the most common number of kids, 1 and 3, are those whom agree most with spanking.
survey %>% 
  mutate(hapmar = as_factor(hapmar)) %>% 
  mutate(hapmar = fct_recode(hapmar,
                               "Pretty Happy" = "PRETTY HAPPY",
                               "Very Happy" = "VERY HAPPY",
                               "Not Too Happy" = "NOT TOO 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) %>% 
 plot_ly(x = ~hapmar) |> 
  add_histogram() 
  1. This histogram shows that the majority of those surveyed are very happy with their marriages.
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()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `class_ = fct_relevel(...)`.
Caused by warning:
! 2 unknown levels in `f`: Middle Class and Upper Class
  1. This histogram shows that the majority of those surveyed were middle class followed by a close second of 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,
                               "Pretty Happy" = "PRETTY HAPPY",
                               "Very Happy" = "VERY HAPPY",
                               "Not Too Happy" = "NOT TOO 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) %>% 
plot_ly(x = ~class_, color = ~hapmar) |> 
  add_histogram()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `class_ = fct_relevel(...)`.
Caused by warning:
! 2 unknown levels in `f`: Middle Class and Upper Class
  1. This histogram shows that the majority of the middle class surveyed are very happy in their marriages, same with working class and upper class, but the lower class have a majority that marked pretty happy.
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"),
                              "30s" =c("30", "31", "32","33", "34", "35","36","37","38","39"),
                              "40s" =c("40","41","42","43","44","45","46","47","48","49"),
                              "50s" =c("50","51","52","53","54","55","56","57","58","59"),
                              "60s" =c("60","61","62","63","64","65","66","67","68","69"),
                              "70s" =c("70","71","72","73","74","75","76","77","78","79"),
                              "80 and up" =c("80","81","82","83","84","85","86","87","88","89 or older"))) %>% 
  drop_na(age) %>% 
  plot_ly(x = ~age) |> 
  add_histogram()
  1. The majority of those surveyed were in their 30s.
survey |> 
  
  mutate(hapmar = as_factor(hapmar)) %>% 
  mutate(hapmar = fct_recode(hapmar,
                               "Pretty Happy" = "PRETTY HAPPY",
                               "Very Happy" = "VERY HAPPY",
                               "Not Too Happy" = "NOT TOO 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(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"),
                              "30s" =c("30", "31", "32","33", "34", "35","36","37","38","39"),
                              "40s" =c("40","41","42","43","44","45","46","47","48","49"),
                              "50s" =c("50","51","52","53","54","55","56","57","58","59"),
                              "60s" =c("60","61","62","63","64","65","66","67","68","69"),
                              "70s" =c("70","71","72","73","74","75","76","77","78","79"),
                              "80 and up" =c("80","81","82","83","84","85","86","87","88","89 or older"))) %>% 
  drop_na(age) %>% 
  

  plot_ly(x = ~age, y = ~hapmar) |> 
  add_histogram2d()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(...)`.
Caused by warning:
! Unknown levels in `f`: 18, 19
  1. This chart shows that the majority of those rating they are very happy in their marriages are in their 30s.