setwd("C:/Users/Owner/Documents/Data science lab work")


library(anesr)

data(timeseries_2016)

anes16 <- timeseries_2016

rm(timeseries_2016)

library('ggplot2')

library('tidyverse')
## -- Attaching packages -------------------------------------------------------------------------------------------------------- tidyverse 1.3.0 --
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## v purrr   0.3.4
## -- Conflicts ----------------------------------------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
women_who_Voted<- filter(anes16,V161002==2,V161005==1) 
thermometers<- select(anes16,V161086, V161087, V161088, V161089, V161089, V161090, V161091, V161092, V161093, V161094, V161095, V161096)
postal_codes<- anes16 %>% 
  
  group_by (V161010e) %>%
  
  summarise (total = n())
## `summarise()` ungrouping output (override with `.groups` argument)
polarized <- anes16 %>% 
  
  mutate (PID = case_when(
   
    V161158x < 4 ~ "Democrat",
    
    V161158x > 4 ~ "Republican",
    
    V161158x == 4 ~ "Independent",)) %>%
   
  
  group_by(PID) %>%
  
  select (129,139) %>%
  
  summarise_all(mean) %>%
  
  pivot_longer(!PID, names_to = "thermometer", values_to = "mean") %>%
  
  pivot_wider(names_from = "PID", values_from = "mean") %>%
  
  mutate (Dr_Diff = Democrat - Republican)
## Adding missing grouping variables: `PID`
neg_val<- function(x)
  
  { ifelse(x < 0, NA, x) }

  NAS<- anes16 %>%
    
  mutate (across(.cols = everything(), neg_val))
states<- poliscidata::states
## Registered S3 method overwritten by 'gdata':
##   method         from  
##   reorder.factor gplots
states$stateid <- trimws(as.character(states$stateid), which = "right")
 
q6 <- merge(x = anes16, y = states, by.x = 'V161010e', by.y = 'stateid')