This assignment is to be handed in as an .R-file through Canvas. Your answer will be reviewed by a teaching assistant as well as two fellow students. Do take the time to condsider the feedback. You will also receive two random answers from fellow students for review. Try to find one positive aspect as well as one suggestion for improvement in each of the answers. You must complete both peer reviews in order to pass the assignment.
The first part of the assignment is intended to give you to practice writing pipes using a Tidyverse data set where there are good online resources. You don’t have to hand in the first part. The second part uses a survey data set, and should be handed in.
Part 1
We’ll use a data set from Tidyverse of all flights departing from New York City. The data set can be installed as a package install.packages("nycflights13"). After calling library(nycflights13) you should have a data set flights available.
You may want to skim over R4DS on transformations. This chapter contains both a good introduction to the flights data as well as a recap on many of the concepts used in class on data wrangling.
library(nycflights13) 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
Speed is distance divided by airtime. Use the planes dataframe (included in the nycflights13-package) to find the fastest and slowest aircraft model measured by speed in average km/h. Your answer should be a tibble containing the fastest and slowest plane
# A tibble: 2 × 2
model avg_speed_kmh
<chr> <dbl>
1 CL-600-2B19 501.
2 777-222 777.
Part 2: Survey data
For this homework you will practice your data wrangling skills using a survey data set that you can download here: data-ESS8NO.dta. The data comes from the European Social Survey, and is available at [europeansocialsurvey.org] in various formats. Further, the data from ESS contains many variables with abbreviated/encoded names. The file contains survey responses from Norway in 2016. To get you started with the assignment, you may use the commands below to read in the data set. You may need to install the package foreign first in order to use the read.dta function.
The variables of interest prtvtbno, agea, rlgdgr and hinctnta are defined in the attached documentation, together with more information on the actual questions. To keep it simple, we rename them to party, age, religiosity and income_decile.
Provide summary statistics of age of respondents split by the party the respondents voted for last election. Who has the oldest/youngest voters? Where is the standard deviation of voters age the largest? Do not report numbers for parties with less than 25 respondents.
The variables religiosity, income_decile and party are encoded as factors (take a look at ?factor for en explanation). Further, they contain some non-responses such as “Don’t know”, “Refusal” and “No answer”. Find a method for filtering out all the non-responses. How many respondents did not provide an eligible response to each of the questions? How many answered both the party and income question?
Filter out all ineligible responses for both income and party. Calculate the average religiosity of each party. Provide your answer as a data frame with one row pr. party, sorted by average religiosity.
(Slightly trickier!) For each party with more than 75 voters, calculate the ratio of the number of respondents in income deciles 9 and 10 over the number of respondents in income deciles 1 and 2. Which party has the highest high- to low-income voters?
For completeness: When working with survey data, we usually have to apply weights to ensure estimates are representative of the population. This is because a survey sample may be a non-random sample of the general population. The survey data set provides the weights. You don’t have to worry about weights in this assignment, but please store the link “survey data -> weights?!?” in your mind for future work.