---
title: "Worksheet 1"
output:
  html_notebook: default
  pdf_document: default
---


## Author: [Put your name here]

## Discussants: [Place the names of anyone you discussed this worksheet with here]



$\\$



**Introduction:** The purpose of these exercises are to start becoming familiar with R and to become familiar with a few basebasell statistics. Please complete these exercises by filling in the code inside the R chunks and writing answers below your code. Some functions you will use to complete these exercises are: sqrt(), c(), sum(), <-, get.Lahman.batting.data(), my.data.frame$my.variable, plot() and basic math operators (e.g., *, -, /, +). 


To create a pdf document from this RMarkdown file click on the 'Knit PDF' button in R Studio, and turn in a pdf copy to Moodle once you have answered all the questions. You can learn more about RMarkdown at [http://rmarkdown.rstudio.com/authoring_basics.html](http://rmarkdown.rstudio.com/authoring_basics.html). This assignment is due at 11:59pm on Sunday January 31st. As discussed, it is important to turn all assignments in on time (even if you can't answer every question). If you have questions please post them to the class Piazza site [(https://piazza.com/class/iy3nflk2izi6np)](https://piazza.com/class/iy3nflk2izi6np), that way everyone in the class can benefit from seeing the answer. 




$\\$



<!-- This R chunk sets some parameters that will be used in the rest of the document. -->
```{r message=FALSE, warning=FALSE, tidy=TRUE, echo = FALSE}
    library(knitr)
    # makes sure the code is wrapped to fit when it creats a pdf
    opts_chunk$set(tidy.opts=list(width.cutoff=60)) 
    set.seed(1)  # set the random number generator to always give the same sequence of random numbers
    source('/home/shared/baseball_stats_2017/baseball_class_functions.R')
```







**Exercise 1:** Let's get started by using R as a calculator. Use R to calculate the square root of 4.333 divided by 1.234. 
```{r message=FALSE, warning=FALSE, tidy=TRUE}

    # delete the below lines and replace with the correct math
    (2 + 3)^2

```



$\\$



**Exercise 2:** David Ortiz hit 28, 32, 29, 23, 30, 35, 37 and 28 home runs over the last 8 seasons. Assign these values to a vector called Ortiz.HR, then multiply this vector by 2 to see how many he would have hit if he was twice as good.
```{r message=FALSE, warning=FALSE, tidy=TRUE}


    
```




$\\$




**Exercise 3:** Now use the sum() function to calculate the total number of home runs David Ortiz hit over the last 8 seasons
```{r message=FALSE, warning=FALSE, tidy=TRUE}


```



$\\$





![Clint Hurdle](http://cardboardgods.files.wordpress.com/2009/01/2007_1023_0001_clint_hurdle_79_360.jpg?w=252&h=360)
$\newline$
Baseball card of Clint Hurdle from 1979




$\\$




**Exercise 4:** Chapter 2 of Big Data Baseball describes Clint Hurdle's career as a baseball player (long before he became the manager of the Pirates). Let's take a look at Clint's batting statistics ourselves. Use the function I wrote called get.Lahman.batting.data() to get a data frame that has Clint's data, and save data frame to a variable called Clint.data. Then display the vector of hits Clint had each year over his career (hint: use the $ symbol). What are the most hits Clint had in a season? Note: if you run the command to get the data frame clint.data in the R console, you can use the View() function to display the data in R studio - however the View() function does not work inside an RMarkdown document.   
```{r message=FALSE, warning=FALSE, tidy=TRUE}




```


**Answer:** 




$\\$


**Exercise 5:** Batting average (BA) is a statistic that is calculated by taking the number of hits a player has (H) and dividing it by the number of 'at bats' a player has (AB). The data frame we created already has Clint's batting average for each year, however see if you can recreate it by taking the hits from the data frame and dividing it by his at bats. Does your recreation match what is in the BA column of the clint.data data frame? Report what the highest BA Clint had in a season. Also, calculate Clint's career batting average by first summing all the hits he had over his whole career and dividing by the sum of all the at bats he had over his whole career, and then report what his career batting is. 
```{r message=FALSE, warning=FALSE, tidy=TRUE}


# Comparing Clint's BA from the data frame to manually created BA



# Clint's career BA



```

**Answers:** 




$\\$



**Exercise 6:** Now use the plot(x, y, type = "o") function to plot Clint's BA as a function of year. Does Clint's BA change a lot from year to year? What might contribute to this variation? 
```{r message=FALSE, warning=FALSE, tidy=TRUE}



```


**Answer:** 




