---
title: "Packages and Basic Programming"
output:
ioslides_presentation:
smaller: true
---
## R Packages
- Commonly used R functions are installed with base R
- R packages containing more specialized R functions can be installed freely from CRAN servers using function `install.packages()`
- After packages are installed, their functions can be loaded into the current R session using the function `library()`
## Installation Demo
We install packages using the `install.packages()` command:
```{r,eval = F}
install.packages("plyr")
```
You can install multiple packages at once using the `c()` command:
```{r,eval = F}
install.packages(c("dplyr","data.table"))
```
Note that when you install packages other dependancy packages may be installed as well.
## Finding R Packages
- How do I locate a package with the desired function?
- Google ("R project" + search term works well)
- R website task views to search relevent subjects: http://cran.r-project.org/web/views/
- ??searchterm will search R help for pages related to the search term
- `sos` package adds helpful features for searching for packages related to a particular topic
## Handy R Packages
- `ggplot2`: Statistical graphics
- `dplyr`/`tidyr`: Manipulating data structures
- `lme4`: Mixed models
- `knitr`: integrate LaTeX, HTML, or Markdown with R for easy reproducible research
## Creating Your Own Functions Outline
Code Skeleton:
```{r}
foo <- function(arg1, arg2, ...) {
# Code goes here
return(output)
}
```
Example: Finding the mean of set of numbers
```{r}
mymean <- function(data) {
ans <- sum(data) / length(data)
return(ans)
}
```
```{r}
mymean(1:5)
```
## If/Else Statements
Code Skeleton:
```{r, eval=FALSE}
if (condition) { # Starting bracket if statement
# Some code that runs if condition is TRUE
} else { # Starting bracket else statement
# Some code that runs if condition is FALSE
}# Ending bracket else statement
```
Example: Finding the mean of set of numbers with a conditional statement
```{r}
mymean <- function(data) { # Starting bracket for loop
if (!is.numeric(data)) { # Starting bracket if statement
stop("Numeric input is required")
} else {
ans <- sum(data) / length(data) # computing division
return(ans)
} # Ending bracket if statement
}# Ending bracket for loop
```
This new function ensures that our argument is a number to continue to the calculations.
## Looping
- Reducing the amount of typing we do can be nice
- If we have a lot of code that is essentially the same we can take advantage of looping.
- R offers several loops: for, while, repeat.
Code Skeleton:
```{r, eval=FALSE}
for (i in Indexset) { # Starting bracket for loop
# Do something
} # Ending bracket for loop
```
##
Example: Printing first 5 players drafted in 2008
```{r}
nba <- read.csv("NBA Draft Class.csv")
for (i in 1:5) { # Indexset is {1,2,3,4,5}
print(nba$Player[i], max.levels = 0) # Print statement
# max.levels = 0 surpresses output of all the levels.
}
```
## For Loops More Involved Example
Our indexing set can be elements aside from numbers.
```{r}
id <- c("Total.Points", "Minutes", "Games")
# Loops through id and prints out each string
for (colname in id) {
print(colname)
}
for(colname in id) {
print(paste(colname, mymean(nba[, colname])))
# paste() is used to print variables and strings together
}
```
In the second for loop, we are cycling though our indexset and printing the column name along with its associated average.
## While Loops
While loops are similar to for loops by stop once a certain condition is met unlike a for loop that continues until the index set is done being iterated through.
## Motivating Example
Constructing a while loop to print out first 5 draft picks in nba data set
```{r}
pick <- 1 # Initialize our starting Value
while (pick <= 5) {
print(nba$Player[pick], max.levels = 0) # Prints each value while hiding the level.
pick <- pick + 1 # Add one to pick
}
```
Having done the same operation with a for loop, you can decide which is easier to use.
## Your Turn
1. Create a function that takes numeric input and provides the mean and a 95% confidence interval for the mean for the data (Hint: The `t.test()` function computes both)
2. Construct checks to your function to make sure the data is either numeric. If it is not, stop the program.
3. Add on a (for or while) loop to your function. Apply this to the Points Per Game and Win Share of the nba data set.
## Answers
### 1.
```{r}
mean.interval <- function(data) {
output <- t.test(data)
return(output)
}
```
##
### 2.
```{r}
mean.interval <- function(data) {
if (!is.numeric(data)) {
stop("Data input is required")
} else {
output <- t.test(data)
return(output)
}
}
```
##
### 3.
```{r}
mean.interval(nba$Points.Per.Game)
```
##
```{r}
mean.interval(nba$Win.Share)
```