---
title: "Introduction To R"
author: "Wells"
date: "3/15/2018"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Get Organized  
***
***

### Create a folder for this project on your Desktop  

  + Copy the data - arkansas crime 2-15-18.csv into that folder
  + Create an R Project in that folder
  + Create an R Markdown html document to describe your steps
  + Create an R Script File to write your steps

***

You can type out or copy the code in this tutorial into RStudio’s console (bottom left window) line by line but the point is to reproduce this in the future. That means you should put all your code in a script in the top left window. File > New File > R Script  
***

### See this slide deck about R  

> <https://docs.google.com/presentation/d/1zICxR7qDM3RQ2Nxi5CqHlM3H8I7qoVkNtqcNcnbbDCw/edit#slide=id.gb2f956a38_0_14>

***

###Here is some basic information about R  
There are four main windows:  

  + Script writing, R Markdown, Table Viewer: Upper Left  
  + Environment - data loaded in R: Upper Right  
  + Console - write commands in R: Lower Left  
  + File Manager and Html table viewer: Bottom Right  
  
***
  
In the Console window, type:
  + 'demo()' for some demos  
  + 'help()' for on-line help  
  + 'help.start()' for an HTML browser interface to help
  + 'q()' to quit R.  
  
  

***

### Load Libraries  
***
Libraries are bits of software you will have to load each time into R to make things run. Here, we will **load library(tidyverse) and library (readxl)**:  

  
```{R}
library(tidyverse)
library (readxl)
```

### Read the Data

Now, you will **load the Arkansas Crime data into R** and check it out.  


```{R}
Crime <- read.csv("Arakansas_crime.csv", stringsAsFactors=FALSE)
```
***
Here's what happened: You assigned a data file by the name of "Crime" and had R read this sucker into memory. The "strings as factors" argument is needed to correctly populate the .csv into the R dataframe.

***
Now, let's look at it.  

```{R Crime}
View(Crime)
```
Note that this data table has sortable headers. Pretty goddamned nice.  

### How many rows?  
```{R}
nrow(Crime)
```
### Let's look at the first five rows

```{R}
head(Crime)
```

###Summary Statistics
Here is a quick way to view the range of your data  

```{R}
summary(Crime)
```

###Time Out: More Introductory Material  
> Here is a great slide deck on R 
> <https://docs.google.com/presentation/d/1O0eFLypJLP-PAC63Ghq2QURAnhFo6Dxc7nGt4y_l90s/edit#slide=id.g1bc441664e_0_93>  

***

###Back to the Exercise:  
Sort by column Murder descending  

```{R}
Crime <- Crime[order(-Crime$Murder),]
View(Crime)
```

###Create a New Column and a Formula. 
Add Murder and Robbery 
```{R}
Crime$Murder.Robbery <- Crime$Murder + Crime$Robbery
View(Crime)
```

###Calculations on columns 
Here we will calculate the total murders and create a new variable to store the results.
```{R}
murder_total <- sum(Crime$Murder)  
murder_avg <- mean(Crime$Murder)  
murder_median <- median(Crime$Murder)
```

###To See the Results
```{R}
murder_total  
murder_avg  
murder_median  
```


  
\textbf{#DEBUGGING THE MATERIAL BELOW #

##NEED TO WORK ON THIS MORE - CREATE A COLUMN AND FORMULA

-Examples from this URL  
> <https://docs.google.com/presentation/d/1O0eFLypJLP-PAC63Ghq2QURAnhFo6Dxc7nGt4y_l90s/edit#slide=id.gb3215a5c4_1_67>

  

#Create new column with a formula (Convert OT column into numeric first)
Crime$Murder_Rape <- gsub("\\$", "", Crime$Murder_Rape)
Crime$Murder_Rape <- as.numeric(Crime$Murder_Rape)

### trying with this now
dataset$New.Column <- dataset$Column subtracted by dataset$Another.Column



##what is this function
gtable_add_cols(x, widths, pos = -1)


#FORMULA TIME (If the columns were numbers in the first place, you could skip the steps above)
earnings$Total.minus.OT <- earnings$TOTAL.EARNINGS - earnings$OVERTIME
earnings$Total.minus.OT <- earnings$TOTAL.EARNINGS - earnings$OVERTIME


#Filter out a column (in R, it's called "subset")
fire_dept <- subset(earnings, DEPARTMENT.NAME=="Boston Fire Department")






Save file

head(merged)

#Save it as a csv
write.csv(merged, "merged.csv")



###Debug this later - trying to get the Crime table to display as an object in R Markdown {R Crime}
Summary(Crime)
}