#install.packages(readr)
#install.packages(dplyr)

library(readr)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
VoterData <- read_csv("~/Tsukasa/NY/CUNY/Class/Spring 2019/Programming for Social Research/VoterData2017(1).csv")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   redovote2016_t_2017 = col_character(),
##   job_title_t_2017 = col_character(),
##   izip_2016 = col_character(),
##   presvote16post_t_2016 = col_character(),
##   second_chance_t_2016 = col_character(),
##   race_other_2016 = col_character(),
##   healthcov_t_2016 = col_character(),
##   employ_t_2016 = col_character(),
##   pid3_t_2016 = col_character(),
##   religpew_t_2016 = col_character(),
##   votemeth16_rnd_2016 = col_character(),
##   presvote16post_rnd_2016 = col_character(),
##   vote2016_cand2_rnd_2016 = col_character(),
##   Clinton_Rubio_rnd_2016 = col_character(),
##   Clinton_Cruz_rnd_2016 = col_character(),
##   Sanders_Trump_rnd_2016 = col_character(),
##   Sanders_Rubio_rnd_2016 = col_character(),
##   second_chance_rnd_2016 = col_character(),
##   obamaapp_rnd_2016 = col_character(),
##   fav_grid_row_rnd_2016 = col_character()
##   # ... with 121 more columns
## )
## See spec(...) for full column specifications.
## Warning: 13 parsing failures.
##  row                      col           expected actual                                                                                       file
## 1418 religpew_muslim_baseline 1/0/T/F/TRUE/FALSE     90 '~/Tsukasa/NY/CUNY/Class/Spring 2019/Programming for Social Research/VoterData2017(1).csv'
## 1531 child_age7_1_baseline    1/0/T/F/TRUE/FALSE     6  '~/Tsukasa/NY/CUNY/Class/Spring 2019/Programming for Social Research/VoterData2017(1).csv'
## 1531 child_age8_1_baseline    1/0/T/F/TRUE/FALSE     4  '~/Tsukasa/NY/CUNY/Class/Spring 2019/Programming for Social Research/VoterData2017(1).csv'
## 1531 child_age9_1_baseline    1/0/T/F/TRUE/FALSE     2  '~/Tsukasa/NY/CUNY/Class/Spring 2019/Programming for Social Research/VoterData2017(1).csv'
## 2947 religpew_muslim_baseline 1/0/T/F/TRUE/FALSE     2  '~/Tsukasa/NY/CUNY/Class/Spring 2019/Programming for Social Research/VoterData2017(1).csv'
## .... ........................ .................. ...... ..........................................................................................
## See problems(...) for more details.
NewVoterData <- VoterData %>%
         select(ft_rep_2017, ft_altright_2017, ft_immig_2017) %>%
         rename("Feelings toward Republicans"=ft_rep_2017, "Feelings toward the alt right"
                =ft_altright_2017, "Feelings toward immigrants"=ft_immig_2017) %>%
         mutate(`Feelings toward Republicans`=ifelse(`Feelings toward Republicans`>100,NA,`Feelings toward Republicans`), 
                `Feelings toward the alt right`=ifelse(`Feelings toward the alt right`>100, NA,`Feelings toward the alt right`),
                `Feelings toward immigrants`=ifelse(`Feelings toward immigrants`>100, NA,`Feelings toward immigrants`))
NewVoterData %>%
  filter(!is.na(`Feelings toward Republicans`)) %>%
  group_by(`Feelings toward Republicans`) %>%
  summarize(`Feelings toward the alt right` = mean(`Feelings toward the alt right`, na.rm = TRUE), `Feelings toward immigrants` = 
              mean(`Feelings toward immigrants`, na.rm = TRUE))
## # A tibble: 101 x 3
##    `Feelings toward Republ~ `Feelings toward the al~ `Feelings toward immi~
##                       <dbl>                    <dbl>                  <dbl>
##  1                        0                     4.46                   70.7
##  2                        1                     8.34                   69.0
##  3                        2                     9.04                   74.6
##  4                        3                     7.03                   73.1
##  5                        4                    12.1                    71.6
##  6                        5                    11.7                    68.5
##  7                        6                    12.9                    72.5
##  8                        7                    17.7                    72.2
##  9                        8                    16.0                    67.4
## 10                        9                    12.2                    68.2
## # ... with 91 more rows
NewVoterData %>%
  filter(!is.na(`Feelings toward the alt right`)) %>%
  group_by(`Feelings toward the alt right`) %>%
  summarize(`Feelings toward Republicans` = mean(`Feelings toward Republicans`, na.rm = TRUE), `Feelings toward immigrants` = 
              mean(`Feelings toward immigrants`, na.rm = TRUE))
## # A tibble: 101 x 3
##    `Feelings toward the alt~ `Feelings toward Repub~ `Feelings toward immi~
##                        <dbl>                   <dbl>                  <dbl>
##  1                         0                    14.9                   73.6
##  2                         1                    18.5                   71.3
##  3                         2                    21.1                   70.8
##  4                         3                    20.3                   67.3
##  5                         4                    26.1                   67.5
##  6                         5                    30.7                   66.7
##  7                         6                    31.1                   69.8
##  8                         7                    30.4                   66.8
##  9                         8                    43.3                   64.1
## 10                         9                    36                     60.0
## # ... with 91 more rows
NewVoterData %>%
  filter(!is.na(`Feelings toward immigrants`)) %>%
  group_by(`Feelings toward immigrants`) %>%
  summarize(`Feelings toward Republicans` = mean(`Feelings toward Republicans`, na.rm = TRUE), `Feelings toward the alt right` = 
              mean(`Feelings toward the alt right`, na.rm = TRUE))
## # A tibble: 101 x 3
##    `Feelings toward immig~ `Feelings toward Repub~ `Feelings toward the al~
##                      <dbl>                   <dbl>                    <dbl>
##  1                       0                    47.1                     40.7
##  2                       1                    52.1                     45.0
##  3                       2                    43.2                     30.7
##  4                       3                    63.9                     43.2
##  5                       4                    51.7                     33.3
##  6                       5                    62.6                     57  
##  7                       6                    55.4                     39.2
##  8                       7                    50.6                     35.2
##  9                       8                    57.5                     39.2
## 10                       9                    52.5                     46.3
## # ... with 91 more rows