Practicing the tidyverse and exploratory data analysis

Part 1: Exploratory data analysis of Star Wars

The starwars data frame comes pre-loaded with the dplyr package. This dataset includes characteristics of the characters in the movie series Star Wars.

Step 1 Get to know the data

Use two tidyverse functions that are built to help you learn more about a data frame to explore starwars data.

  • What does each row represent? Include your response in a comment inside the code chunk.
# Your code here

Step 2 Summary statistics

Write code to create a tibble with the mean, median, and standard deviation of the numerical variables in the starwars data frame. Manipulate the code as needed to get a value for each summary statistic.

  • Which variable has the most spread in it’s distribution? Include your response as a comment in the code chunk.
# Your code here

Step 3 Plot aesthetics

Run the code chunk below. Modify the plot to change the color of all points to the color red.

  • What does the plot tell you about the relationship between character height and mass? Include your response as a comment in the code chunk.
ggplot(starwars, 
       aes(x = height, y = mass, color = gender, size = birth_year)) +
  geom_point()

Step 4 More aesthetics

With the plot from the previous step, make all points the color “#30509C” and:

  • Add labels for a title, x and y axes, and the size of points (legend title) to the plot below.
  • Select a theme and add code to change the plot theme.
# Your code here

Step 5

Pick a numerical variable from the dataset and create a histogram to observe the distribution. Select a reasonable binwidth. Include a chart title, x-axis label, and y-axis label.

# Your code here

Step 6

Pick a numerical variable and a categorical variable and make a visualization (you pick the type!) to visualize the relationship between the two variables. Include a title, x-axis label, and y-axis label. Along with your code and output, provide an interpretation of the visualization.

# Your code here

Step 7

Pick a single categorical variable from the data set and make a bar plot of its distribution. Exclude all NAs in the variable. Include a title, x-axis label, and y-axis label.

# Your code here

Part 2: General Social Survey

The General Social Survey (GSS) gathers data on contemporary American society in order to monitor and explain trends and constants in attitudes, behaviors, and attributes. Hundreds of trends have been tracked since 1972. In addition, since the GSS adopted questions from earlier surveys, trends can be followed for up to 70 years.

The GSS contains a standard core of demographic, behavioral, and attitudinal questions, plus topics of special interest. Among the topics covered are civil liberties, crime and violence, intergroup tolerance, morality, national spending priorities, psychological well-being, social mobility, and stress and traumatic events.

In this section you will analyze data from the 2016 GSS, using it to estimate values of population parameters of interest about US adults.

The data can be found in the dsbox package, and it’s called gss16. You can find out more about the dataset by inspecting its documentation.

Load the dsbox package.

# Your code here

Step 1 Explore the gss16 dataset

In one code chunk:

  • Write code to add the gss16 dataset to your Environment pane.

  • Write code to open the data set in the Source pane.

  • Write code to explore the dataset using tidyverse functions.

  • Include the number of observations and variables as a comment in the code chunk.

# Your code here

Step 2 Summarize the gss16 dataset

Write code to create a tibble with the mean, median, and standard deviation of the numerical variables in the data frame. Manipulate the code as needed to get a value for each summary statistic.

# Your code here

Step 3 Explore relationships between numerical variables the gss16 dataset

Create a scatter plot of years of education and hours spent on email each week, set the color of all points to the color blue. Include a chart title.

# Your code here

Step 4

What are the possible values for the polviews variable? Use a tidyverse function.

# Your code here

Harassment at work

In 2016, the GSS added a new question on harassment at work. The question is phrased as the following.

Over the past five years, have you been harassed by your superiors or co-workers at your job, for example, have you experienced any bullying, physical or psychological abuse?

Answers to this question are stored in the harass5 variable in our dataset.

Step 5

What are the possible responses to this question and how many respondents chose each of these answers? Use a tidyverse function.

# Your code here

Step 6

What percentage of the respondents for whom this question is applicable (i.e. excluding NAs and Does not applys) have been harassed by their superiors or co-workers at their job?

  • Use tidyverse functions.

  • Include the answer as a comment in the code chunk.

# Your code here

Time spent on email

The 2016 GSS also asked respondents how many hours and minutes they spend on email weekly. The responses to these questions are recorded in the emailhr and emailmin variables. For example, if the response is 2.5 hrs, this would be recorded as emailhr = 2 and emailmin = 30.

Step 7

Create a new variable called email that combines these two variables to report the number of minutes the respondents spend on email weekly.

# Your code here

Step 8

Visualize the distribution of this new variable. Find the mean and the median of the number of minutes respondents spend on email weekly. Use tidyverse functions.

# Your code here

Step 9

What are the possible responses to the question Last week were you working full time, part time, going to school, keeping house, or what? and how many respondents chose each of these answers? Note that this information is stored in the wrkstat variable. Use a tidyverse function.

# Your code here

Political views and science research

The 2016 GSS also asked respondents whether they think of themselves as liberal or conservative (polviews) and whether they think science research is necessary and should be supported by the federal government (advfront).

  • The question on science research is worded as follows:

Even if it brings no immediate benefits, scientific research that advances the frontiers of knowledge is necessary and should be supported by the federal government.

And possible responses to this question are Strongly agree, Agree, Disagree, Strongly disagree, Don’t know, No answer, Not applicable.

  • The question on political views is worded as follows:

We hear a lot of talk these days about liberals and conservatives. I’m going to show you a seven-point scale on which the political views that people might hold are arranged from extremely liberal–point 1–to extremely conservative–point 7. Where would you place yourself on this scale?

The levels of this variables are spelled inconsistently: “Extremely liberal” vs. “Extrmly conservative”. Since this is the spelling that shows up in the data, you need to make sure this is how you spell the levels in your code.

And possible responses to this question are Extremely liberal, Liberal, Slightly liberal, Moderate, Slghtly conservative, Conservative, Extrmly conservative. Responses that were originally Don’t know, No answer and Not applicable are already mapped to NAs upon data import.

Step 10

In a new variable, recode advfront such that Strongly Agree and Agree are mapped to "Yes", and Disagree and Strongly disagree are mapped to "No". The remaining levels can be left as is. Overwrite the data frame to include the new variable, but don’t overwrite the existing advfront variable, instead pick a different, informative name for your new variable.

# Your code here

Step 11

In a new variable, recode polviews such that Extremely liberal, Liberal, and Slightly liberal, are mapped to "Liberal", and Slghtly conservative, Conservative, and Extrmly conservative are mapped to "Conservative". The remaining levels (categories) can be left as is.

  • Don’t overwrite the existing polviews, instead pick a different, informative name for your new variable.
# Your code here