---
title: 'Code for QSS tidyverse Chapter 1: Introduction'
author: "Kosuke Imai and Nora Webb Williams"
date: "First Printing"
output:
  pdf_document: default
always_allow_html: true
---

# Introduction

```{r setup1, include = FALSE, purl=FALSE, eval = T}
library(knitr)
library(kableExtra)
library(tidyverse)
opts_chunk$set(tidy = FALSE,
        fig.align = "center",
        background = "#EEEEEE",
        fig.width = 7,
        fig.height = 7,
        out.width = ".7\\linewidth",
        out.height = ".7\\linewidth",
        cache = TRUE)
options(width = 60)
set.seed(12345)
```

## Overview of the Book

## How to Use this Book

## Introduction to R and the tidyverse

### Arithmetic Operations: R as a Calculator


```{r}
5 + 3
```


```{r}
5 - 3
5 / 3
5 ^ 3
5 * (10 - 3)
sqrt(4)
```


### R Scripts


```{r, eval=FALSE}
## This is the start of an R Script
## The heading provides some information about the file

## File name: testing_arithm.R
## Author: Kosuke Imai and Nora Webb Williams
## Purpose: Practicing basic math commands and commenting in R
##

5 - 3 # What is 5 minus three?
5 / 3
5 ^ 3
5 * (10 - 3) # A bit more complex
sqrt(4) # This function will take the square root of a number
```

### Loading Packages


```{r eval=FALSE}
install.packages("devtools") # install the package
library(devtools) # load the package
## install a package from github
devtools::install_github("kosukeimai/qss-package", build_vignettes = TRUE)
## You may need to allow R to update/install additional packages
```

```{r}
## load the qss package
library("qss")
```


```{r, message=FALSE, warning=FALSE}
library(tidyverse) #if this command does not work, remember to install the package
```

### Objects


```{r}
result <- 5 + 3
result
print(result)
```

```{r}
result <- 5 - 3
result
```


```{r eval=TRUE, error=TRUE}
Result
```


```{r}
kosuke <- "instructor"
kosuke
```

```{r}
kosuke <- "instructor and author"
kosuke
```


```{r}
Result <- "5"
Result
```


```{r error=TRUE}
Result / 3
sqrt(Result)
```


```{r}
result
class(result)
Result
class(Result)
class(sqrt)
```

### Vectors


```{r}
world.pop <- c(2525779, 3026003, 3691173, 4449049, 5320817, 6127700, 6916183)
world.pop
```


```{r}
pop.first <- c(2525779, 3026003, 3691173)
pop.second <- c(4449049, 5320817, 6127700, 6916183)
pop.all <- c(pop.first, pop.second)
pop.all
```


```{r}
world.pop[2]
world.pop[c(2, 4)]
world.pop[c(4, 2)]
world.pop[-3]
```


```{r}
pop.million <- world.pop / 1000
pop.million
```


```{r}
pop.rate <- world.pop / world.pop[1]
pop.rate
```


```{r}
pop.increase <- world.pop[-1] - world.pop[-7]
percent.increase <- (pop.increase / world.pop[-7]) * 100
percent.increase
```


```{r}
percent.increase[c(1, 2)] <- c(20, 22)
percent.increase
```

### Functions

```{r}
length(world.pop)
min(world.pop)
max(world.pop)
range(world.pop)
mean(world.pop)
sum(world.pop) / length(world.pop)
```


```{r}
year <- seq(from = 1950, to = 2010, by = 10)
year
```


```{r}
seq(to = 2010, by = 10, from = 1950)
```


```{r}
seq(from = 2010, to = 1950, by = -10)
2008:2012
2012:2008
```


```{r}
names(world.pop)
names(world.pop) <- year
names(world.pop)
world.pop
```


```{r eval = FALSE}
myfunction <- function(input1, input2, ..., inputN) {

    DEFINE 'output' USING INPUTS

    return(output)
}
```


```{r}
my.summary <- function(x){ # function takes one input, x
  s.out <- sum(x)
  l.out <- length(x)
  m.out <- s.out / l.out
  out <- c(s.out, l.out, m.out) # define the output
  names(out) <- c("sum", "length", "mean") # add labels
  return(out) # end function by calling output
}
z <- 1:10 # z is a vector from 1 to 10
my.summary(z) # run my.summary function on z
my.summary(world.pop) # run my.summary function on world.pop
```


### Data Files: Loading and Subsetting


```{r, eval=FALSE}
getwd() # Check what your current working directory is
setwd("qss/INTRO") # Set your working directory with a path
getwd() # Check that you changed your working directory
```


```{r, eval=FALSE}
## If your working directory is where the .csv file is stored
UNpop <- read_csv("UNpop.csv")
class(UNpop) # What type of object is UNpop?
```


```{r, eval= FALSE}
load("UNpop.RData")
```

```{r message=FALSE, warning=FALSE, eval=FALSE}
## Specifying a relative path to find and read in UNpop.csv
## Will overwrite previously loaded UNpop object
UNpop <- read_csv("INTRO/UNpop.csv")
class(UNpop) # what type of object is UNpop?
```


```{r}
## Load the package
library(qss)
## Load the UN pop data
## Will overwrite previously loaded UNpop object
data(UNpop, package = "qss")
```


```{r}
names(UNpop)
nrow(UNpop)
ncol(UNpop)
dim(UNpop)
```


```{r}
UNpop$world.pop
```


```{r}
## subset all rows for the column called "world.pop" from the UNpop data
UNpop[, "world.pop"]
## subset the first three rows (and all columns)
UNpop[c(1, 2, 3),]
## subset the first three rows of the "year" column
UNpop[1:3, "year"]
```


```{r}
## Subset the first three rows of UNpop with tidyverse
slice(UNpop, n = 1:3)
```

```{r}
## Extract/subset the world.pop variable (column)
select(UNpop, world.pop)
```


```{r}
## Base R subset the first three rows of the year variable
UNpop[1:3, "year"]
## or in tidyverse, combining slice() and select()
select(slice(UNpop, 1:3), year)
```


```{r}
UNpop %>% # take the UNpop data we have loaded, and then...
  slice(1:3) %>% # subset the first three rows, and then...
  select(year) # subset the year column
```


```{r}
UNpop %>%
  slice(seq(1, n(), by = 2)) %>% # using a sequence from 1 to n()
  select(world.pop)
```


```{r}
UNpop %>%
  filter(row_number() %% 2 == 1) %>%
  select(world.pop)
```

```{r}
pop.1970 <- UNpop %>% # take the UNpop data and then....
  filter(year == 1970) %>% # subset rows where the year variable is equal to 1970
  select(world.pop) %>% # subset just the world.pop column
  pull() # return a vector, not a tibble

## Print the vector to the console to see it
print(pop.1970)
```

### Data Files: Adding Variables

```{r}
UNpop.mill <- UNpop %>% # create a new tibble from UNpop
  mutate(world.pop.mill = world.pop / 1000) %>% # create a new variable, world.pop.mill
  select(-world.pop) # drop the original world.pop column
```


```{r}
## Adding a nonsense variable to the UNpop.mill data
UNpop.mill <- UNpop.mill %>%
  mutate(nonsense.var = world.pop.mill / year)
```


```{r}
## Adding a variable with if_else
UNpop.mill <- UNpop.mill %>%
  mutate(after.1980 = if_else(year >= 1980, 1, 0))
```


```{r}
## Creating a vector of the years of interest
specific.years <- c(1950, 1980, 2000)

## Adding a variable with if_else and %in%
UNpop.mill <- UNpop.mill %>%
  mutate(year.of.interest = if_else(year %in% specific.years, 1, 0))
```

### Data Frames: Summarizing

```{r}
summary(UNpop.mill)
mean(UNpop.mill$world.pop.mill)
```


```{r}
## Add a row where values for all columns is NA
UNpop.mill.wNAs <- UNpop.mill %>%
  add_row(year = NA, world.pop.mill = NA,
          nonsense.var = NA, after.1980 = NA,
          year.of.interest = NA)
## Take the mean of world.pop.mill (returns NA)
mean(UNpop.mill.wNAs$world.pop.mill)
## Take the mean of world.pop.mill (ignores the NA)
mean(UNpop.mill.wNAs$world.pop.mill, na.rm = TRUE)
```


```{r}
UNpop.mill %>%
  summarize(mean.pop = mean(world.pop.mill),
            median.pop = median(world.pop.mill))
```


```{r, message = FALSE, warning=FALSE}
UNpop.mill %>%
  group_by(after.1980) %>% # create subset group for each value of after.1980
  summarize(mean.pop = mean(world.pop.mill)) # calculate mean for each group
```

### Saving Objects

```{r, eval=FALSE}
save.image("qss/INTRO/Chapter1.RData")
```


```{r, eval=FALSE}
save(UNpop, file = "Chapter1.RData")
save(world.pop, year, file = "qss/INTRO/Chapter1.RData")
```

```{r, eval=FALSE}
load("Chapter1.RData")
```


```{r, eval=FALSE}
write_csv(UNpop, path = "INTRO/UNpop.csv")
```

### Loading Data in Other Formats

```{r, eval=FALSE, tidy=TRUE}
## install packages -- note the syntax for multiple packages at once
install.packages(c("foreign", "haven", "rio"))
```

```{r, warning=FALSE, eval = FALSE}
library("foreign") # load package
library("haven")
library("rio")
```


```{r, eval=FALSE}
read.dta("UNpop.dta")
read.spss("UNpop.sav")
```

```{r eval=FALSE}
UNpop_dta_url <- "https://github.com/kosukeimai/qss/raw/master/INTRO/UNpop.dta"

UNpop <- read_dta(UNpop_dta_url)

## reading in with import; note that each UNpop <- will override the prior object
UNpop <- import("https://github.com/kosukeimai/qss/raw/master/INTRO/UNpop.csv")

UNpop <- import("https://github.com/kosukeimai/qss/raw/master/INTRO/UNpop.RData")

UNpop <- import("https://github.com/kosukeimai/qss/raw/master/INTRO/UNpop.dta")
```


```{r, eval=FALSE}
write.dta(UNpop, file = "UNpop.dta")
write_dta(UNpop, "UNpop.dta")
```

### Programming and Learning Tips


```{r, eval=FALSE}
##
## File: UNpop.R
## Author: Kosuke Imai and Nora Webb Williams
## The code loads the UN population data, adds a variable,
## and saves the data as a STATA file
##

## Load the necessary packages
library(haven)
library(tidyverse)
library(qss)

## Load the UN pop data
data(UNpop, package = "qss")

## Replace the raw population with the population in millions
UNpop <- UNpop %>%
  mutate(world.pop = world.pop / 1000 )

## Save the data as a .dta file
write_dta(UNpop, path = "UNpop.dta")
```


```{r, eval=FALSE}
source("UNpop.R")
```

