First, we’ll load the data into a dataframe object:
df <- read.csv(url('https://raw.githubusercontent.com/fivethirtyeight/data/master/poll-quiz-guns/guns-polls.csv'), stringsAsFactors = F)
Next, we’ll take a peek at the dataset:
str(df)
## 'data.frame': 57 obs. of 9 variables:
## $ Question : chr "age-21" "age-21" "age-21" "age-21" ...
## $ Start : chr "2/20/18" "2/27/18" "3/1/18" "2/22/18" ...
## $ End : chr "2/23/18" "2/28/18" "3/4/18" "2/26/18" ...
## $ Pollster : chr "CNN/SSRS" "NPR/Ipsos" "Rasmussen" "Harris Interactive" ...
## $ Population : chr "Registered Voters" "Adults" "Adults" "Registered Voters" ...
## $ Support : int 72 82 67 84 78 72 76 41 44 43 ...
## $ Republican.Support: int 61 72 59 77 63 65 72 69 68 71 ...
## $ Democratic.Support: int 86 92 76 92 93 80 86 20 20 24 ...
## $ URL : chr "http://cdn.cnn.com/cnn/2018/images/02/25/rel3a.-.trump,.guns.pdf" "https://www.ipsos.com/en-us/npripsos-poll-majority-americans-support-policies-aimed-keep-guns-out-hands-dangerous-individuals" "http://www.rasmussenreports.com/public_content/lifestyle/general_lifestyle/march_2018/americans_favor_raising_a"| __truncated__ "http://thehill.com/opinion/civil-rights/375993-americans-support-no-gun-under-21" ...
head(df)
## Question Start End Pollster Population Support
## 1 age-21 2/20/18 2/23/18 CNN/SSRS Registered Voters 72
## 2 age-21 2/27/18 2/28/18 NPR/Ipsos Adults 82
## 3 age-21 3/1/18 3/4/18 Rasmussen Adults 67
## 4 age-21 2/22/18 2/26/18 Harris Interactive Registered Voters 84
## 5 age-21 3/3/18 3/5/18 Quinnipiac Registered Voters 78
## 6 age-21 3/4/18 3/6/18 YouGov Registered Voters 72
## Republican.Support Democratic.Support
## 1 61 86
## 2 72 92
## 3 59 76
## 4 77 92
## 5 63 93
## 6 65 80
## URL
## 1 http://cdn.cnn.com/cnn/2018/images/02/25/rel3a.-.trump,.guns.pdf
## 2 https://www.ipsos.com/en-us/npripsos-poll-majority-americans-support-policies-aimed-keep-guns-out-hands-dangerous-individuals
## 3 http://www.rasmussenreports.com/public_content/lifestyle/general_lifestyle/march_2018/americans_favor_raising_age_to_buy_gun_not_enlist_or_vote
## 4 http://thehill.com/opinion/civil-rights/375993-americans-support-no-gun-under-21
## 5 https://poll.qu.edu/national/release-detail?ReleaseID=2525
## 6 https://d25d2506sfb94s.cloudfront.net/cumulus_uploads/document/zq33h2ipcl/econTabReport.pdf
For now, we only want to see the overall, republican, and democratic support levels for each of the questions. We’ll use the dplyr package to reduce the data to look at the average support by question:
library(dplyr)
reduced <- group_by(df, Question) %>%
summarize(overall = mean(Support),
republican = mean(Republican.Support),
democratic = (mean(Democratic.Support)))
reduced
## # A tibble: 8 x 4
## Question overall republican democratic
## <chr> <dbl> <dbl> <dbl>
## 1 age-21 75.9 67 86.4
## 2 arm-teachers 42 72.2 17.2
## 3 background-checks 87.4 83.6 91.9
## 4 ban-assault-weapons 61.8 42.6 80.7
## 5 ban-high-capacity-magazines 67.3 52.7 83.9
## 6 mental-health-own-gun 85.8 86.7 87.5
## 7 repeal-2nd-amendment 10 5 15
## 8 stricter-gun-laws 66.5 46.4 86.9
We can also include the population, which shows whether the respondents are registered voters:
reduced_pop <- group_by(df, Question, Population) %>%
summarize(overall = mean(Support),
republican = mean(Republican.Support),
democratic = (mean(Democratic.Support))) %>%
arrange(Question, Population)
reduced_pop
## # A tibble: 15 x 5
## # Groups: Question [8]
## Question Population overall republican democratic
## <chr> <chr> <dbl> <dbl> <dbl>
## 1 age-21 Adults 74.5 65.5 84
## 2 age-21 Registered Voters 76.4 67.6 87.4
## 3 arm-teachers Adults 42.7 69 20.7
## 4 arm-teachers Registered Voters 41.3 75.3 13.7
## 5 background-checks Adults 84.5 77.5 92.5
## 6 background-checks Registered Voters 88.6 86 91.6
## 7 ban-assault-weapons Adults 62.5 48.5 77
## 8 ban-assault-weapons Registered Voters 61.6 41.4 81.4
## 9 ban-high-capacity-magazines Adults 73 59 88
## 10 ban-high-capacity-magazines Registered Voters 66.3 51.7 83.2
## 11 mental-health-own-gun Adults 92 88 96
## 12 mental-health-own-gun Registered Voters 84.6 86.4 85.8
## 13 repeal-2nd-amendment Registered Voters 10 5 15
## 14 stricter-gun-laws Adults 70 51 88.5
## 15 stricter-gun-laws Registered Voters 65.7 45.3 86.6