class: center, middle

<!-- 
background-image: url(figures/fig_dist_examples.png)
background-position: 50% 90%
background-size: 750px
-->

```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
knitr::opts_chunk$set(comment     = NA,
                      echo        = TRUE, 
                      warning     = FALSE, 
                      message     = FALSE,
                      fig.align   = "center",   # center all figures
                      fig.width   = 8,          # set default figure width to 4 inches
                      fig.height  = 5)        # set default figure height to 3 inches
```

## "R is the bomb." 
### -- Everyone


---
# Why R?

.pull-left[.huge[
Why Use `R`?
]
.large[
- .coral[**Free**]
- Almost always up-to-date
- Best .bluer[data visualizations]
- .nicegreen[Syntax oriented] (easy to reproduce analyses)
- Gets updated regularly
- You can make your .gray1[own functionality] (e.g., `table1()`)
]]

.pull-right[.huge[
Any Issues?
]
.large[
- Learning curve
- Too extensive (people often focus on too much)
- Advisor's sometimes don't use it

The pro's heavily outweight the con's *if* you are willing to use it. It can save you tons of time in the long-run.
]]

---
# How R Works

.huge[
Uses Objects and Functions
]

.large[.large[
- R sees data as an .nicegreen[**object**]
- Uses .bluer[**functions**] to do stuff to those objects
  - Lots of functions are included in outside .salmon[**packages**]
  - We will use several from what has come to be known as the *tidyverse*
]]






---
.pull-left[
## .coral[Objects]

.large[.large[
Virtual objects are like physical objects (e.g., a car is good to travel in, a table not so much)

`R` uses virtual objects (`data.frame`, `vectors`)
]]

```{r, eval=FALSE}
x <- c(5,3,4,1)
```
```{r, eval=FALSE}
x = c(5,3,4,1)
```

]

--

.pull-right[
## .bluer[Vectors] and .nicegreen[Data Frames]

.large[.large[
- a `vector` is a single variable (can be numeric, character, or factor)
- a `data.frame` is a group of vectors (variables)
]]

```{r, echo=FALSE}
df <- data.frame(
  x = c(5,3,4,1),
  y = c(1,1,0,0)
)
```

```{r, comment=">"}
df
```

]

---
# Functions

.huge[Its how we do stuff in `R`]

.pull-left[.large[

Generally looks like:

- `my_fun(arg1, arg2)`
    - `my_fun` is the function's name
    - `()` surround the arguments that go in the function
    -`arg1` etc. are the pieces of information we give a function
]]

--

.pull-right[
.large[Here's an example of the "mean" function:

- Using our `df` data.frame from before
    - we grab a single variable from `df` using `$`
- `mean()` wants a single variable and it provides the average
]
```{r}
## This is a comment
mean(df$x)
```
]

---
# Important first steps in `R`

.large[.large[
1. Read in Data
2. Quickly assess the data
3. Clean the data
4. Analyze the data
]]

--

.large[We'll show each of these over the next few weeks starting with reading in, assessing and cleaning the data]


---
# Read in the data

.huge[Data comes in various files:]

.pull-left[.large[
- CSV
- tab-delimited
- SPSS
- Excel
]]
.pull-right[.large[
- SAS
- Stata
- Mplus
- etc.
]]

--
### `R` can read all types

--

.huge[Generally, it all works in a similar way]

---
# Read in data


- .large[If your data is called `my_data_file.csv`, then:]

```{r, eval = FALSE}
my_data <- rio::import("my_data_file.csv")
```

--

- .large[If your data is called `my_data_file.xlsx`, then:]

```{r, eval = FALSE}
my_data <- rio::import("my_data_file.xlsx")
```

--

- .large[If your data is an SPSS file, then:]

```{r, eval = FALSE}
my_data <- rio::import("my_data_file.sav")
```


---
# Steps for .dcoral[Preparing] the Data for Analysis

.large[.large[
1. .coral[Get the data]
2. .nicegreen[Prep the data]
    - Variable Labels 	
    - Value Labels	
    - Missing Values
3. .bluer[Compute] new variables and values (fill in missing codes, recode scores, categorize/group values, combine)
4. Get .dcoral[descriptives] using `tableF()` and `table1()` from the `furniture` package to check what is going on
]]

---
# Step 1: Get the data

```{r, eval=FALSE}
## Data is in .sav form (SPSS)
library(readr)
d <- rio::import("Data/GSS_reduced_example.csv")
d
```

```{r, echo=FALSE}
library(readr)
d <- rio::import("~/GoogleDrive/3 Teaching/EDUC 7610/GSS_Data/Data/GSS_reduced_example.csv")
d
```


---
# Step 2: Prep the data

```{r, message=FALSE, warning=FALSE}
## install.packages("tidyverse")
library(tidyverse)
d_clean <- d %>%
  janitor::clean_names() %>%
  janitor::remove_empty("cols") %>%
  janitor::remove_empty("rows") %>%
  dplyr::mutate(marital = factor(marital)) %>%
  dplyr::mutate(degree  = factor(degree)) %>%
  dplyr::mutate(partyid = factor(partyid))
d_clean
```


---
# Step 3: Compute new variables/values

```{r}
d_clean <- d_clean %>%
  dplyr::mutate(natspend = furniture::rowmeans(natcity, natcrime, natdrug, nateduc, 
                                               natspac, natenvir, natheal, natrace, 
                                               natarms, nataid, natfare, natroad, 
                                               natsoc, natmass, natpark, natchld, natsci))
```

```{r, echo=FALSE}
d_clean %>%
  dplyr::select(natspend, natcity, natcrime, natdrug, nateduc, 
                natspac, natenvir, natheal, natrace, 
                natarms, nataid, natfare, natroad, 
                natsoc, natmass, natpark, natchld, natsci)
```


---
# Step 4: Get descriptives

.pull-left[
```{r, message=FALSE, warning=FALSE}
library(furniture)
d_clean %>%
  furniture::tableF(degree)
```
]

---
# Step 4: Get descriptives

.pull-left[
```{r, message=FALSE, warning=FALSE}
d_clean %>%
  furniture::tableF(marital)
```
]


---
# Step 4: Get descriptives

```{r}
library(furniture)
d_clean %>%
  dplyr::group_by(degree) %>%
  furniture::table1(natspend, natcity)
```




---
# Ways to make it easier

.huge[
.salmon[Focus on what is necessary] and don't worry about the rest

Use the homework as .bluer[opportunities to learn]

Use the .nicegreen[resources] around you

- In class examples
- Google
- Online Books (see [tysonbarrett.com](tysonbarrett.com))
]


---
class: inverse, center, middle

# Next Topic

### Chapter 1


