LailaKGSSAssignment

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)                   # a graphics package, and alternative to ggplot2 

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.xlsx")
glimpse(survey)
Rows: 8,054
Columns: 8
$ year     <dbl> 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2006, 2…
$ id_      <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18…
$ childs   <chr> "3", "1", "2", "1", "0", "2", "1", "0", "0", "2", "2", "4", "…
$ age      <chr> "50", "27", "67", "50", "20", "35", "29", "32", "23", "32", "…
$ race     <chr> "Black", "Other", "White", "Black", "Black", "Other", "Black"…
$ hapmar   <chr> ".i:  Inapplicable", ".i:  Inapplicable", ".i:  Inapplicable"…
$ class_   <chr> "Working class", "Working class", "Middle class", "Working cl…
$ spanking <chr> ".i:  Inapplicable", "STRONGLY AGREE", "DISAGREE", "AGREE", "…
survey |> 
  count(race)
# A tibble: 4 × 2
  race                  n
  <chr>             <int>
1 .i:  Inapplicable    53
2 Black              1199
3 Other              1004
4 White              5798
survey |> 
  mutate(race = as_factor(race)) |> 
  
  mutate(race = fct_recode(race,
                      
                          NULL = ".i:  Inapplicable"))  |>
                  
  
  drop_na(race) |> 
  
  count(race)
# A tibble: 3 × 2
  race      n
  <fct> <int>
1 Black  1199
2 Other  1004
3 White  5798
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()

The above histogram demonstrates the race of participants.

survey |> 
  count(spanking)
# A tibble: 8 × 2
  spanking                           n
  <chr>                          <int>
1 .d:  Do not Know/Cannot Choose    43
2 .i:  Inapplicable               3686
3 .n:  No answer                     4
4 .s:  Skipped on Web                7
5 AGREE                           1780
6 DISAGREE                        1036
7 STRONGLY AGREE                   966
8 STRONGLY DISAGREE                532
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) |> 
  
  count(spanking)
# A tibble: 4 × 2
  spanking              n
  <fct>             <int>
1 STRONGLY AGREE      966
2 DISAGREE           1036
3 AGREE              1780
4 STRONGLY DISAGREE   532
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)) |> 
  
  plot_ly(x = ~spanking) |> 
  add_histogram()
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 = as.numeric(spanking)) |> 
  
  count(spanking)
# A tibble: 4 × 2
  spanking     n
     <dbl> <int>
1        1   966
2        2  1036
3        3  1780
4        4   532
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 = as_factor(race)) |> 
   mutate(race = fct_recode(race,
                            
                            NULL = ".i:  Inapplicable")) |>
                            
   drop_na(spanking) |> 
  
  mutate(spanking = as.numeric(spanking)) |> 
  

plot_ly(x = ~spanking, y = ~race) |> 
  add_boxplot()
Warning: Ignoring 31 observations
survey |> 
  count(childs)
# A tibble: 11 × 2
   childs                             n
   <chr>                          <int>
 1 .d:  Do not Know/Cannot Choose    13
 2 .i:  Inapplicable                 11
 3 0                               2323
 4 1                               1255
 5 2                               2061
 6 3                               1257
 7 4                                631
 8 5                                235
 9 6                                124
10 7                                 68
11 8 or more                         76
survey |> 
  mutate(childs = as_factor(childs)) |> 
  
  mutate(childs = fct_recode(childs,
                        
  
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".i:  Inapplicable")) |> 
  
  drop_na(childs) |> 
  
  count(childs)
# A tibble: 9 × 2
  childs        n
  <fct>     <int>
1 3          1257
2 1          1255
3 2          2061
4 0          2323
5 4           631
6 7            68
7 8 or more    76
8 5           235
9 6           124
survey |> 
  mutate(childs = as_factor(childs)) |> 
  
  mutate(childs = fct_recode(childs,
                      
                          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 = as.numeric(childs)) |> 
  
  count(childs)
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `childs = fct_relevel(...)`.
Caused by warning:
! 1 unknown level in `f`: 8
# A tibble: 9 × 2
  childs     n
   <dbl> <int>
1      1  2323
2      2  1255
3      3  2061
4      4  1257
5      5   631
6      6   235
7      7   124
8      8    68
9      9    76
survey |> 
  mutate(childs = as_factor(childs)) |> 
  
  mutate(childs = fct_recode(childs,
                          
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".i:  Inapplicable")) |> 
    

  drop_na(childs) |> 
  
  mutate(childs = fct_infreq(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 = ".i:  Inapplicable", 
                          NULL = ".n:  No answer",
                          NULL = ".s:  Skipped on Web")) |> 
  
  
  drop_na(spanking) |> 
  
  mutate(spanking = fct_infreq(spanking)) |> 
  
  
  mutate(childs = as_factor(childs)) |> 
    mutate(childs = fct_recode(childs,
                           NULL = ".d:  Do not Know/Cannot Choose",
                           NULL = ".i:  Inapplicable")) |> 
  
    
  drop_na(childs) |> 
  
  mutate(childs = as.numeric(childs)) |> 
  

plot_ly(x = ~spanking, y = ~childs) |> 
  add_boxplot()
survey |> 
  count(hapmar)
# A tibble: 7 × 2
  hapmar                             n
  <chr>                          <int>
1 .d:  Do not Know/Cannot Choose     2
2 .i:  Inapplicable               5167
3 .n:  No answer                    17
4 .s:  Skipped on Web                3
5 NOT TOO HAPPY                    109
6 PRETTY HAPPY                    1014
7 VERY HAPPY                      1742
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) |> 
  
  count(hapmar)
# A tibble: 3 × 2
  hapmar            n
  <fct>         <int>
1 PRETTY HAPPY   1014
2 VERY HAPPY     1742
3 NOT TOO HAPPY   109
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) |> 
  
  mutate(hapmar = fct_infreq(hapmar)) |> 
  
  plot_ly(x = ~hapmar) |> 
  add_histogram()
survey |> 
  count(class_)
# A tibble: 8 × 2
  class_                             n
  <chr>                          <int>
1 .d:  Do not Know/Cannot Choose    40
2 .i:  Inapplicable               1518
3 .n:  No answer                     9
4 .s:  Skipped on Web                7
5 Lower class                      571
6 Middle class                    2827
7 Upper class                      257
8 Working class                   2825
survey |> 
  mutate(class_ = as_factor(class_)) |> 
  
  mutate(class_ = fct_recode(class_,
                          
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".i:  Inapplicable",
                          NULL = ".n:  No answer",
                          NULL = ".s:  Skipped on Web")) |> 
  
  drop_na(class_) |> 
  
  count(class_)
# A tibble: 4 × 2
  class_            n
  <fct>         <int>
1 Working class  2825
2 Middle class   2827
3 Lower class     571
4 Upper class     257
survey |> 
  mutate(class_ = as_factor(class_)) |> 
  
  mutate(class_ = fct_recode(class_,
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".i:  Inapplicable",
                          NULL = ".n:  No answer",
                          NULL = ".s:  Skipped on Web")) |> 

  drop_na(class_) |> 
  
  mutate(class_ = fct_infreq(class_)) |> 
  
  count(class_)
# A tibble: 4 × 2
  class_            n
  <fct>         <int>
1 Middle class   2827
2 Working class  2825
3 Lower class     571
4 Upper class     257
survey |> 
  mutate(class_ = as_factor(class_)) |> 
  
  mutate(class_ = fct_recode(class_,
                          NULL = ".d:  Do not Know/Cannot Choose",
                          NULL = ".i:  Inapplicable",
                          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 = ".i:  Inapplicable",
                          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 = ".i:  Inapplicable",
                          NULL = ".n:  No answer",
                          NULL = ".s:  Skipped on Web")) |> 

  
  drop_na(hapmar) |> 

  plot_ly(x = ~class_, color = ~hapmar) |> 
  add_histogram()
survey |> 
  count(age)
# A tibble: 73 × 2
   age                n
   <chr>          <int>
 1 .n:  No answer   226
 2 18                41
 3 19                76
 4 20                98
 5 21                98
 6 22               118
 7 23               116
 8 24               106
 9 25               119
10 26               141
# ℹ 63 more rows
survey |> 
  mutate(age = as_factor(age)) |> 
  
  mutate(age = fct_recode(age,
                        
                      NULL = ".n:  No answer")) |> 
  
  drop_na(age) |> 
  
  count(age)
# A tibble: 72 × 2
   age       n
   <fct> <int>
 1 50      133
 2 27      132
 3 67       89
 4 20       98
 5 35      138
 6 29      140
 7 32      173
 8 23      116
 9 81       40
10 47      163
# ℹ 62 more rows
survey |> 
  mutate(age = as_factor(age)) |> 
  
  mutate(age = fct_recode(age,
                          
                            NULL = ".n:  No answer")) |> 

  drop_na(age) |> 
  
  mutate(age = fct_infreq(age)) |> 
  
  count(age)
# A tibble: 72 × 2
   age       n
   <fct> <int>
 1 36      174
 2 32      173
 3 39      167
 4 42      166
 5 47      163
 6 40      157
 7 33      157
 8 48      155
 9 44      155
10 34      152
# ℹ 62 more rows
survey |> 
  mutate(age = as_factor(age)) |> 
  
  mutate(age = fct_recode(age,
                            NULL = ".n:  No answer")) |> 
  
   mutate(age = fct_collapse(age,
                              "Other" = c("Under 30s",
                                          "30s",
                                          "40s",
                                          "50s",
                                          "60s",
                                          "70s",
                                          "80 and up"))) |> 
  drop_na(age) |> 
  
  mutate(age = fct_infreq(age)) |> 
  
  plot_ly(x = ~age) |> 
  add_histogram()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(...)`.
Caused by warning:
! Unknown levels in `f`: Under 30s, 30s, 40s, 50s, 60s, 70s, 80 and up
survey |> 
  
   mutate(age = as_factor(age)) |> 
  
  mutate(age = fct_recode(age,
                             NULL = ".n:  No answer")) |> 
  
   mutate(age = fct_collapse(age,
                               "Other" = c("Under 30s",
                                          "30s",
                                          "40s",
                                          "50s",
                                          "60s",
                                          "70s",
                                          "80 and up"))) |> 
  drop_na(age) |> 
  
  mutate(age = fct_infreq(age)) |> 
  
  
  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 = ~age, y = ~hapmar) |> 
  add_histogram2d()
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `age = fct_collapse(...)`.
Caused by warning:
! Unknown levels in `f`: Under 30s, 30s, 40s, 50s, 60s, 70s, 80 and up