Homework 1

Due Date: Tuesday, 13 February by 23:59:59

Submission Instructions: Submit your completed R script file to Moodle.

This homework will be relatively short and straight-forward. The goal is to ease you into R now so that you are ready to complete some of the more complex data analysis that will take place later.

Question #1:

Create an R script and save it with an appropriate name. Add a header to your R script file in the format below.

# Name: [first_name] [last_name]
# Date: [date]
# Description: [brief description of the file] 

# Question 2:

Question #2:

In your R script file, load the tidyverse package. Show the code used.

Question #3:

Create a vector with the following set of numbers: \({30, 60, 90, 120, 150}\). Perform the following operations, showing the code used for each.

Part A: Multiply the vector by 2. In a brief comment, tell me what the result was.

Part B: Take the vector and divide it by 3. Tell me what the result was in a brief comment.

Part C: Multiply the vector by itself. Tell me what the result was in a brief comment.

Part D: Return the third element of the vector.

Part E: Replace the second element of the vector with a missing value (NA).

Part F: Sum the vector, excluding the missing value. In a comment, write the answer.

Question #4:

Using the socviz package (see section 2.3 of the course textbook), load the election dataset into a new object called elec. Complete the following tasks, showing the code used for each.

Part A: Find the total popular vote received by Gary Johnson using the johnson_vote variable.

Part B: Find the total popular vote received by ‘Other’ candidates using the other_vote variable.

Part C: In a comment answer the following question: who received more votes, Gary Johnson or “other” candidates? By how much?

Part D: Use the sum() function on the state variable. In a brief comment, explain why this didn’t work and what the error message is telling you.