```{r, echo=FALSE}
tips <- read.csv("http://heike.github.io/rwrks/01-r-intro/data/tips.csv")
tips$rate <- tips$tip / tips$total_bill
```

## R is an Overgrown Calculator

- Follow along (copy & paste the code into the console):
```{r, eval = FALSE}
curl::curl_download(
  "https://raw.githubusercontent.com/heike/rwrks/gh-pages/summerschool/01-Introduction-to-R/code/2-basics.R",
  "2-basics.R"
)
file.edit("2-basics.R")
```

## R is an Overgrown Calculator


```{r}
# Addition and Subtraction
2 + 5 - 1

# Multiplication
109*23452

# Division
3/7
```


## More Calculator Operations

```{r}
# Integer division
7 %/% 2

# Modulo operator (Remainder)
7 %% 2

# Powers
1.5^3
```

## Math Functions

- Exponentiation 
    - `exp(x)`
- Logarithms
    - `log(x)`
    - `log(x, base = 10)`
- Trigonometric functions
    - `sin(x)`
    - `asin(x)`
    - `cos(x)`
    - `tan(x)`
    
## Creating Objects

We can create an object using the assignment operator `<-`:

```{r}
x <- 5
todays_date <- 21
```

We can then perform any of the functions on these objects:

```{r}
log(x)
todays_date^2
```

## Rules for Variable Creation

- Variable names can't start with a number
- Variables in R are case-sensitive
- Some common letters are used internally by R and should be avoided as variable names (c, q, t, C, D, F, T, I)
- There are reserved words that R won't let you use for variable names. (for, in, while, if, else, repeat, break, next)
- R will let you use the name of a predefined function __without any warning__.  Try not to overwrite those though!

## Vectors

A variable usually consists of more than a single value. We can create a **vector** using the `c` (combine) function:

```{r}
y <- c(1, 5, 3, 2)
```

Operations will then be done element-wise:

```{r}
y / 2
```

## Getting Help

We will talk MUCH more about vectors in a bit, but for now, let's talk about a couple ways to get help. The primary function to use is the `help` function. Just pass in the name of the function you need help with:

```{r, eval=FALSE}
help(head)
```

The `?` function also works:

```{r, eval=FALSE}
?head
```

Googling for help is a bit hard. Searches of the form  R + CRAN + <your query> usually get good results

## R Reference Card

We will pass you out a copy, but you can download the reference card from:

http://cran.r-project.org/doc/contrib/Short-refcard.pdf

Having this open or printed off and near you while working is helpful.

## Your Turn {data-background=#527a7a}

Using the R Reference Card (and the Help pages, if needed), do the following:

1. Find out how many rows and columns the `iris' data set has.  Figure out at least 2 ways to do this.
*Hint: "Variable Information" section on the first page of the reference card!*

2. Use the `rep` function to construct the following vector:  1 1 2 2 3 3 4 4 5 5
*Hint: "Data Creation" section of the reference card*

3. Use `rep` to construct this vector: 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5

## Data Frames Introduction

- `tips` is a data frame.
- Data frames hold data sets
- Columns can have different types - like an Excel spreadsheet
- Each column in a data frame is a vector - so each column needs to have values that are all the same type.
- We can access different columns using the `$` operator.

```{r}
tip <- tips$tip
bill <- tips$total_bill
```

## More about Vectors

A vector is a list of values that are all the same type. We have seen that we can create them using the `c` or the `rep` function. We can also use the `:` operator if we wish to create consecutive values:

```{r}
a <- 10:15
a
```

We can extract the different elements of the vector like so:

```{r}
bill[3]
```

## Indexing Vectors

We have seen that we can access individual elements of the vector. But **indexing** is a lot more powerful than that:

```{r}
head(tip)

tip[c(1, 3, 5)]
tip[1:5]
```

## Logical Values

- R has built in support for logical values
- TRUE and FALSE are built in.  T (for TRUE) and F (for FALSE) are supported but can be modified
- Logicals can result from a comparison using
    - $<$
    - $>$
    - $<=$
    - $>=$
    - $==$
    - $!=$
    
## Indexing with Logicals

We can index vectors using logical values as well:

```{r}
x <- c(2, 3, 5, 7)
x[c(TRUE, FALSE, FALSE, TRUE)]
x > 3.5
x[x > 3.5]
```

## Logical Examples

```{r}
rate <- tips$rate
head(rate)
sad_tip <- rate < 0.10
rate[sad_tip]
```

## Your Turn {data-background=#527a7a}

1. Find out how many people tipped over 20%.

*Hint: if you use the `sum` function on a logical vector, it'll return how many TRUEs are in the vector:*
```{r}
sum(c(TRUE, TRUE, FALSE, TRUE, FALSE))
```
2. **More Challenging**: Calculate the sum of the total bills of anyone who tipped over 20%

## Modifying Vectors

We can modify vectors using indexing as well:

```{r}
x <- bill[1:5]
x
x[1] <- 20
x
```

## Vector Elements

Elements of a vector must all be the same type:

```{r}
head(rate)
rate[sad_tip] <- ":-("
head(rate)
```

By changing a value to a string, all the other values got changed as well.

## Data Types in R

- Can use `mode` or `class` to find out information about variables
- `str` is useful to find information about the structure of your data
- Many data types: numeric, integer, character, Date, and factor most common
    
```{r}
str(tips)
```

## Converting Between Types

We can convert between different types using the `as` series of functions:

```{r}
size <- head(tips$size)
size
as.character(size)
as.numeric("2")
```

## Some useful functions

There are a whole variety of useful functions to operate on vectors. A couple of the more common ones are `length`, which returns the length (number of elements) of a vector, and `sum`, which adds up all the elements of a vector.

```{r}
x <- tip[1:5]
length(x)
sum(x)
```

## Statistical Functions

Using the basic functions we've learned it wouldn't be hard to compute some basic statistics.

```{r}
(n <- length(tip))
(meantip <- sum(tip) / n)
(standdev <- sqrt(sum((tip - meantip)^2) / (n - 1)))
```

But we don't have to.

## Built-in Statistical Functions

```{r}
mean(tip)
sd(tip)
summary(tip)
quantile(tip, c(.025, .975))
```

## Element-wise Logical Operators

- `&` (elementwise AND)
- `|` (elementwise OR)

```{r}
c(T, T, F, F) & c(T, F, T, F)
c(T, T, F, F) | c(T, F, T, F)
# Which are big bills with a poor tip rate?
id <- (bill > 40 & rate < .10)
tips[id,]
```

## Your Turn {data-background=#527a7a}

1. Read up on the dataset (`?diamonds`)
2. Plot price by carat (use qplot - go back to the motivating example for help with the syntax)
3. Create a variable `ppc` for price/carat. Store this variable as a column in the diamonds data
4. Make a histogram of all ppc values that exceed $10000 per carat.
5. Explore any other interesting relationships you find
