This page will contain all the assignments you submit for the class.

Created: 2021-09-19, Last compiled: 2022-06-07

Instructions for all assignments

I want you to submit your assignment as a PDF, so I can keep a record of what the code looked like that day. I also want you to include your answers on your personal GitHub website. This will be good practice for editing your website and it will help you produce something you can keep after the class is over.

  1. Download the Assignment1.Rmd file from Canvas. You can use this as a template for writing your answers. It’s the same as what you can see on my website in the Assignments tab. Once we’re done with this I’ll edit the text on the website to include the solutions.

  2. On RStudio, open a new R script in RStudio (File > New File > R Script). This is where you can test out your R code. You’ll write your R commands and draw plots here.

  3. Once you have finalized your code, copy and paste your results into this template (Assignment 1.Rmd). For example, if you produced a plot as the solution to one of the problems, you can copy and paste the R code in R markdown by using the ``{r} ``` command. Answer the questions in full sentences and Save.

  4. Produce a PDF file with your answers. To do this, knit to PDF (use Knit button at the top of RStudio), locate the PDF file in your docs folder (it’s in the same folder as the Rproj), and submit that on on Canvas in Assignment 1.

  5. Build Website, go to GitHub desktop, commit and push. Now your solutions should be on your website as well.

Assignment 1

Collaborators: Lorem Ipsum.

This assignment is due on Canvas on Monday 9/20 before class, at 10:15 am. Include the name of anyone with whom you collaborated at the top of the assignment.

Problem 1

Install the datasets package on the console below using install.packages("datasets"). Now load the library.

library(datasets)

Answer: I’ve loaded the library!

Load the USArrests dataset and rename it dat. Note that this dataset comes with R, in the package datasets, so there’s no need to load data from your computer. Why is it useful to rename the dataset?

dat <- USArrests # this line is renaming the data

Answer: Well, we want to replicate analyses. That’s why it’s nice to rename data.

Problem 2

Use this command to make the state names into a new variable called State.

dat$state <- tolower(rownames(USArrests))

This dataset has the state names as row names, so we just want to make them into a new variable. We also make them all lower case, because that will help us draw a map later - the map function requires the states to be lower case.

List the variables contained in the dataset USArrests.

names(dat)
## [1] "Murder"   "Assault"  "UrbanPop" "Rape"     "state"
# Hey professor, the four variables are Murder, Assault, UrbanPop, Rape.

Problem 3

What type of variable (from the DVB chapter) is Murder?

Answer:

What R Type of variable is it?

Answer:

Problem 4

What information is contained in this dataset, in general? What do the numbers mean?

Answer:

Problem 5

Draw a histogram of Murder with proper labels and title.

Problem 6

Please summarize Murder quantitatively. What are its mean and median? What is the difference between mean and median? What is a quartile, and why do you think R gives you the 1st Qu. and 3rd Qu.?

Problem 7

Repeat the same steps you followed for Murder, for the variables Assault and Rape. Now plot all three histograms together. You can do this by using the command par(mfrow=c(3,1)) and then plotting each of the three.

What does the command par do, in your own words (you can look this up by asking R ?par)?

Answer:

What can you learn from plotting the histograms together?

Answer:

Problem 8

In the console below (not in text), type install.packages("maps") and press Enter, and then type install.packages("ggplot2") and press Enter. This will install the packages so you can load the libraries.

Run this code:

library(maps) 
library(ggplot2) 

ggplot(dat, aes(map_id=state, fill=Murder)) + 
  geom_map(map=map_data("state")) + 
  expand_limits(x=map_data("state")$long, y=map_data("state")$lat)

What does this code do? Explain what each line is doing.

Answer:

\[\\[2in]\]

Assignment 2

(Coming soon)