── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.4 ✔ readr 2.1.4
✔ 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")
Below you will find a histogram that displays the number of different races participating within this data set.
Below is the histogram for spanking. The answers range from strongly agree to strongly disagree in the use of spanking.
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")) |>drop_na(spanking) |>plot_ly(x=~spanking) |>add_histogram()
Below is a combine histogram of different races views on spanking
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")) |>drop_na(spanking) |>mutate(spanking =fct_infreq(spanking)) %>%mutate(race =fct_recode(race,NULL =".i: Inapplicable")) |>drop_na(race) %>%plot_ly(x=~spanking, color =~ race) |>add_histogram()
Below you will find the code used to adjust “spanking: as a numeric variable. Further down, there is a boxplot that displays the relationship between views of spanking and race. Spanking seems to be strongly favored more so by the black population that participated in the study.
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 AGREE","AGREE","DISAGREE","STRONGLY DISAGREE"))) |>drop_na(spanking) |>mutate(spanking =as.numeric(spanking)) |>count(spanking)
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 AGREE","AGREE","DISAGREE","STRONGLY DISAGREE"))) |>mutate(spanking =as.numeric(spanking)) %>%mutate(race =as_factor(race)) |>mutate(race =fct_recode(race,NULL =".i: Inapplicable")) %>%plot_ly(x =~race, y =~spanking) |>add_boxplot()
Warning: Ignoring 3097 observations
The histogram below displays the number of children the participants had over the course of this survey.
survey |>mutate(childs =as_factor(childs)) |>mutate(childs =fct_recode(childs,NULL =".d: Do not Know/Cannot Choose",NULL =".i: Inapplicable","8 or more"="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: `childs = fct_recode(...)`.
Caused by warning:
! Unknown levels in `f`: 8
box plot for comparisons between the “spanking” and “childs”
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 AGREE","AGREE","DISAGREE","STRONGLY DISAGREE"))) |>mutate(childs =as_factor(childs)) |>mutate(childs =recode(childs,NULL =".d: Do not Know/Cannot Choose",NULL =".i: Inapplicable")) |>mutate(childs=as.numeric(childs)) %>%plot_ly(x =~spanking, y =~childs) |>add_boxplot()
Warning: Ignoring 3066 observations
Histogram for participants and their views within their marriage
survey |>mutate(hapmar =as_factor(hapmar)) |>mutate(hapmar =fct_recode(hapmar,NULL =".d: Do not Know/Cannot Choose",NULL =".i: Inapplicable",NULL =".n: No answer",NULL =".s: Skipped on Web")) |>drop_na(hapmar) |>plot_ly(x=~hapmar) |>add_histogram()
Below is a histogram below indicating the different class levels (lower -> upper 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")) |>drop_na(class_) |>mutate(class_=fct_infreq(class_)) %>%mutate(class_ =factor(class_, levels =c("Lower class", "Working class", "Middle class", "Upper class"))) %>%plot_ly(x=~class_) |>add_histogram()
Below you will find comparison of happy marraiges within different working classes. It appears middle class shows higher rates of very happy marraiges.
survey |>mutate(hapmar =fct_recode(hapmar,NULL =".d: Do not Know/Cannot Choose",NULL =".i: Inapplicable",NULL =".n: No answer",NULL =".s: Skipped on Web")) |>drop_na(hapmar) %>%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_ =factor(class_, levels =c("Lower class", "Working class", "Middle class", "Upper class"))) %>%mutate(class_=fct_infreq(class_)) %>%plot_ly(x=~class_, color =~ hapmar) |>add_histogram()
Below is the code used to collapse different age groups for the histogram. It appears more individuals in their 30’s participated within this dataset.