## Instructions
Upload a PDF file, named with your UC Davis email ID and homework number (e.g., `xtai_hw1.pdf`), to Gradescope (accessible through Canvas). You will give the commands to answer each question in its own code block, which will also produce output that will be automatically embedded in the output file. Each answer must be supported by any written statements as well as any code used. 

All code used to produce your results must be shown in your PDF file (e.g., do not use `echo = FALSE` or `include = FALSE` as options anywhere). `Rmd` files do not need to be submitted, but may be requested by the TA and must be available when the assignment is submitted. 

Students may choose to collaborate with each other on the homework, but must clearly indicate with whom they collaborated. 

## Problem 1: Syntax and class-typing (continued) (5 points)
For the following series of commands, either explain their results, or why they should produce errors.

```{r eval = FALSE}
dataframe3 <- data.frame(z1 = "5", z2 = 7, z3 = 12)
dataframe3[1, 2] + dataframe3[1, 3]
```

## Problem 2: Working with a data frame (50 points)
The data set [here](https://xhtai.github.io/statdatasci/homework/homework2/rnf6080.dat) records hourly rainfall at a certain location in Canada, every day from 1960 to 1980.

a. Read the data set into R using the command `read.table()`. Use the help function to learn what arguments this function takes. Once you have the necessary input, read the data set into R and make it a data frame called `rain.df`.

b. What is the structure of the data set? How many rows and columns does `rain.df` have? (If there are not 5070 rows and 27 columns, something is wrong; check the previous part to see what might have gone wrong.)

c. What are the names of the columns of `rain.df`?

d. What is the value of row 5, column 7 of `rain.df`?

e. Display the second row of `rain.df` in its entirety.

f. Explain what this command does:

```{r eval = FALSE}
names(rain.df) <- c("year", "month", "day", paste0("hour", seq(0, 23)))
```

by running it on your data and examining the object. (You may find the display functions `head()` and `tail()` useful here.) 

g. Create a new column in the data frame for the sum of the rightmost 24 columns. Do this using base R and call the column `daily1`. Do this using `dplyr` and call it `daily2`. Check that they are the same. 
  
h. Create a histogram of the values in your new column of daily rainfall values (read the documentation and use the `hist()` command in base R). What is wrong with this picture?

i. Create a new data frame `rain.df.fixed` that takes the original data frame and fixes it for the apparent flaw you have discovered. Having done this, produce a new histogram with the corrected data and explain why this is more reasonable.

## Problem 3: Data manipulation and visualization (45 points)
R includes a number of pre-specified data objects as part of its default installation. We will load and manipulate one of these, a data frame of 93 cars with model year 1993. Begin by ensuring that you can load this data with the commands

```{r}
library(MASS)
data(Cars93)
```


a. What is the structure of the data set? How many rows and columns does `Cars93` have?

b. What types of drive trains are in the data set, and how many of each type? What is the mean price of cars in each of these groups? (Use `dplyr`.)

c. Assuming that these cars are exactly as fuel efficient as this table indicates, find the cars that have the maximum, minimum and median distance travellable for highway driving. You will need at least two columns to work this out; why those two?

d. Create a binary variable for whether the car can take 7 or fewer passengers. Only for cars that take 7 or fewer passengers, use `ggplot2` to create a scatterplot of `Horsepower` vs. `Price`. Change the color to depend on the variable `Type`. Make all the points have a transparency of .5. Add labels for title, x and y axes. What can we learn from this plot about the relationship between horsepower, price, and type of car? 


## Appendix

```{r eval = TRUE}
sessionInfo()
```
