Intro to R

Jeho Park, Stephen Park
August 4, 2018

Agenda

  • About R
  • R Basics
  • Working With Data
  • Hands-On Project
  • Further Learning

This R Workshop is...

  • for newbies.
    • If you already have some experience in R, please raise your hands.
  • about R's programming aspects.
    • It's designed to help you start using and coding R.
  • not about Statistics.
    • I assume that you already have some basic knowledge of Statistics
  • for you to start learning R afterwards.
    • This workshop is designed to give you a nudge to learn more R down the road. There are many resources to learn R in depth.

Learning Objectives:

By the end of this this workshop, you will be able to:

  • Install R libraries/packages.
  • Import and export data from/to a simple CSV file in R.
  • Distinguish the differences between R data objects and convert them.
  • Use subsetting methods to create subsamples.
  • Create basic plots using both base plot in R.
  • Create R markdown documents and R slides.
  • Create tidy data and manipulate those data using dplyr package.

What is R?

  • R is a statical programming language/environment.
  • R is open source and free.
  • R is widely used.
  • R is cross-platform.
  • R is hard to learn (really?).

What is not R?

  • S: R's ancestor
  • S-Plus: Commercial; modern implementation of S
  • SAS: Commercial; widely used in the commercial analytics.
  • STATA: Commercial; widely used by economists.
  • SPSS: Commercial; easy to use; widely used in Social Science.
  • MATLAB: Commercial; can do some Stats.
  • Python: Also can do some Stats; one of the two mostly used programming languages in data science.

Then Why R?

  • R community is active and constantly growing
  • R is one of the most popular stat programming lang
  • R has tons of user generated libraries/packages
  • R code is easily shared with others
  • R is constantly improved

Then Why R?

  • R community is active and constantly growing
  • R is one of the most popular stat tools/programming languages
  • R has tons of user generated libraries/packages
  • R code is easily shared with others
  • R is constantly improved
Further reading about R's popularity in science and engineering:
R moves up to 5th place in IEEE language rankings at https://www.r-bloggers.com/r-moves-up-to-5th-place-in-ieee-language-rankings/
https://spectrum.ieee.org/static/interactive-the-top-programming-languages-2017 (6th in 2017)

Then Why R?

IEEE Spectrum: Popular Programming Languages in 2017 Poplar Programming Languages

Then Why R?

  • R community is active and constantly growing
  • R is one of the most popular stat tools/programming languages
  • R has tons of user generated libraries/packages
  • R code is easily shared with others
  • R is constantly improved

Getting help online and offline

On the Internet:

R Basics

Let's get ready for R-ing!

Open your RStudio app. Open your browser.

Getting Started - RConsole

  • Upon opening RStudio, you will see the Console
    • RConsole

Getting Started - RConsole

  • You can perform some calculations by typing into it.
2+2
[1] 4

Getting Started - Variables

  • You can also use variables and assign them values.
x = 2
x+2
[1] 4
# The "<-" operator is the same as "="
y <- x+1 # "<-" is preferred
x+y
[1] 5

Getting Started - R Environment

  • When you create a variable it will show up on the environment pane in RStudio Environment Pane

R Basics - Arithmetic Operators

  • Operators are symbols that use some input and operate on them.
  • Arithmetic operators are our common mathematical symbols.
x*x # Multiplication
[1] 4
x/y # Division
[1] 0.6666667
x^3 # Exponentiation
[1] 8

R Basics - Logical Operators

  • Logical operators output either True or False
2 < 5  # Less Than
[1] TRUE
x >= 2 # Greater Than Or Equal To
[1] TRUE

R Basics - Logical Operators

x == 1 # Equal to. NOTE: This is different from "="
[1] FALSE
x != x # Not Equal to
[1] FALSE

R Basics - Data Types

  • R can work with more than just numbers.
class("hello, world!") # The class function returns the data type of a variable or value.
[1] "character"
class(x == y) 
[1] "logical"

R Basics - Vectors

  • Up until now, we have been working with single numbers and variables also known as scalars.
  • In R, there are also vectors which are sequences of values.
a <- c(1,2,3)
b <- c(3:1)

for(i in 1:3) {
  print(a[i]+b[i])
}
[1] 4
[1] 4
[1] 4

R Basics - Vectors - 2

  • R's basic data object is “vector”
  • Operators are optimized for vector (element-wise) operations
  • Use vectorized operations than loops whenever possible
a + b
[1] 4 4 4
a^2
[1] 1 4 9

R Basics - Built-In Functions

  • Functions take an input and produce an output.
  • R contains many functions that are built-in, some of which we have already seen such as class() and c().
abs(-5) # Absolute Value
[1] 5
sqrt(4) # Square Root
[1] 2

R Basics - Built-In Functions - 2

sum(a)  # Sum
[1] 6
toupper("hello") # To Upper Case
[1] "HELLO"

R Basics - Built-In Functions - 3

sd(a) # Can you guess what this funtion calc?
[1] 1
cor(a,b) # How about this?
[1] -1

R Basics - Libraries and Packages

  • R is known for its community and its libraries/packages
  • Packages are collections of R functions, data, and compiled code in a well-defined format. The directory where packages are stored is called the library.
  • We will install the dplyr package, a popular package for data manipulation.
  • install.packages('dplyr')
library('dplyr')

R Basics - Help

  • With all these packages and functions you may forget how to use one or need to remember which one to use.
# This will search for usage of the hist function
?hist
# This will search packages and functions about histograms
??histogram

Exercise 1

  • Review of some basic R features
  • Open ./exercises/intro/exercise_1.Rmd (this is a R markdown file)

Working with Data

Working with Data - DataFrames

  • A data frame is used for storing data tables. It is a list of vectors of equal length.
names <- c("John", "Kim", "Terry")
ages  <- c(21, 34, 16)
male  <- c(TRUE, FALSE, TRUE)
data.frame(names, ages, male)
  names ages  male
1  John   21  TRUE
2   Kim   34 FALSE
3 Terry   16  TRUE

Working with Data - DataFrames - Datasets

  • R's datasets package has some built in datasets that we will be using.
  • The CO2 data frame has 84 rows and 5 columns of data from an experiment on the cold tolerance of the grass species Echinochloa crus-galli.
help(CO2)
  • You can use '$' to access a vector (column/feature/etc) of a dataframe

Working with Data - Data Exploration - Head

# The head function shows the first few entries in a dataframe.
head(CO2)
  Plant   Type  Treatment conc uptake
1   Qn1 Quebec nonchilled   95   16.0
2   Qn1 Quebec nonchilled  175   30.4
3   Qn1 Quebec nonchilled  250   34.8
4   Qn1 Quebec nonchilled  350   37.2
5   Qn1 Quebec nonchilled  500   35.3
6   Qn1 Quebec nonchilled  675   39.2

Working with Data - Data Exploration - Summary

# The summary function shows summary statistics.
summary(CO2)
     Plant             Type         Treatment       conc     
 Qn1    : 7   Quebec     :42   nonchilled:42   Min.   :  95  
 Qn2    : 7   Mississippi:42   chilled   :42   1st Qu.: 175  
 Qn3    : 7                                    Median : 350  
 Qc1    : 7                                    Mean   : 435  
 Qc3    : 7                                    3rd Qu.: 675  
 Qc2    : 7                                    Max.   :1000  
 (Other):42                                                  
     uptake     
 Min.   : 7.70  
 1st Qu.:17.90  
 Median :28.30  
 Mean   :27.21  
 3rd Qu.:37.12  
 Max.   :45.50  

Working with Data - Data Visualization

hist(CO2$uptake) # Use help function for more plotting options

Working with Data - Data Visualization - Boxplot

boxplot(CO2$conc)

Working with Data - Data Visualization - Scatterplot

plot(CO2$uptake, CO2$conc)

Exercise 2

  • Review of datasets and data visualization
  • Open ./exercises/intro/exercise_2.Rmd

Working with Data - Data Subsseting (classic) - 1

Operators that can be used to extract subsets of R objects.

  • '[' and ']' always returns an object of the same class as the original; can be used to select more than one element.
  • '[[' and ']]' is used to extract elements of a list or a data frame; it can only be used to extract a single element.
  • $ is used to extract elements of a list or data frame by name.

Working with Data - Data Subsetting (classic) - 2

x <- c("a", "b", "c", "c", "d", "a")
x[1]
x[1:4]
x[x > "a"] 
u <- x > "a" # what's u here?
u
x[u] # subsetting using a boolean vector
y <- list(foo=x, bar=x[u]) 
y
y[[1]]
y$bar

Working with Data - Data subsetting (classic) - 3

  • Create a dataframe containing the first 10 observations.
  • Create a dataframe where only the Plant identifier is Qn3.
  • Create a dataframe only containing observations with the CO2 concentration level greater than 600.
co2_10obs <- CO2[<row>, <col>]
co2_Qn3 <- CO2[?, ?]
co2_gt600conc <- subset(CO2, conc > 600)

Working with Data - Data Manipulation (dplyr)

  • Dplyr is the most common package used for data exploration and transformation
  • Dplyr functions: filter, select, arrange, mutate, summarise (plus group_by)
head(filter(CO2, Treatment=='nonchilled'))
  Plant   Type  Treatment conc uptake
1   Qn1 Quebec nonchilled   95   16.0
2   Qn1 Quebec nonchilled  175   30.4
3   Qn1 Quebec nonchilled  250   34.8
4   Qn1 Quebec nonchilled  350   37.2
5   Qn1 Quebec nonchilled  500   35.3
6   Qn1 Quebec nonchilled  675   39.2

Working with Data - Data Manipulation (dplyr) - Select

  • select will output only the columns that you choose
head(select(CO2, Plant, conc, uptake), 3)
  Plant conc uptake
1   Qn1   95   16.0
2   Qn1  175   30.4
3   Qn1  250   34.8

Working with Data - Data Manipulation (dplyr) - Chaining

  • You can chain/pipe dplyr functions together
    • The infix (or pipe) operator '%>%' will feed the resulting object into the 1st paramater of the next function
x <- select(filter(CO2, Treatment=='nonchilled'), Plant, conc, uptake) # OR
y <- CO2 %>% filter(Treatment=='nonchilled') %>% select(Plant, conc, uptake)
x == y
      Plant conc uptake
 [1,]  TRUE TRUE   TRUE
 [2,]  TRUE TRUE   TRUE
 [3,]  TRUE TRUE   TRUE
 [4,]  TRUE TRUE   TRUE
 [5,]  TRUE TRUE   TRUE
 [6,]  TRUE TRUE   TRUE
 [7,]  TRUE TRUE   TRUE
 [8,]  TRUE TRUE   TRUE
 [9,]  TRUE TRUE   TRUE
[10,]  TRUE TRUE   TRUE
[11,]  TRUE TRUE   TRUE
[12,]  TRUE TRUE   TRUE
[13,]  TRUE TRUE   TRUE
[14,]  TRUE TRUE   TRUE
[15,]  TRUE TRUE   TRUE
[16,]  TRUE TRUE   TRUE
[17,]  TRUE TRUE   TRUE
[18,]  TRUE TRUE   TRUE
[19,]  TRUE TRUE   TRUE
[20,]  TRUE TRUE   TRUE
[21,]  TRUE TRUE   TRUE
[22,]  TRUE TRUE   TRUE
[23,]  TRUE TRUE   TRUE
[24,]  TRUE TRUE   TRUE
[25,]  TRUE TRUE   TRUE
[26,]  TRUE TRUE   TRUE
[27,]  TRUE TRUE   TRUE
[28,]  TRUE TRUE   TRUE
[29,]  TRUE TRUE   TRUE
[30,]  TRUE TRUE   TRUE
[31,]  TRUE TRUE   TRUE
[32,]  TRUE TRUE   TRUE
[33,]  TRUE TRUE   TRUE
[34,]  TRUE TRUE   TRUE
[35,]  TRUE TRUE   TRUE
[36,]  TRUE TRUE   TRUE
[37,]  TRUE TRUE   TRUE
[38,]  TRUE TRUE   TRUE
[39,]  TRUE TRUE   TRUE
[40,]  TRUE TRUE   TRUE
[41,]  TRUE TRUE   TRUE
[42,]  TRUE TRUE   TRUE

Working with Data - Data Manipulation (dplyr) - Arrange

  • arrange() sorts rows by a column
# Use desc() to sort descending
CO2 %>% arrange(desc(uptake)) %>% head(3)
  Plant   Type  Treatment conc uptake
1   Qn3 Quebec nonchilled 1000   45.5
2   Qn2 Quebec nonchilled 1000   44.3
3   Qn3 Quebec nonchilled  675   43.9

Working with Data - Data Manipulation (dplyr) - Mutate

  • mutate() creates new variables
CO2 %>% mutate(conc_L = conc / 1000) %>% head()
  Plant   Type  Treatment conc uptake conc_L
1   Qn1 Quebec nonchilled   95   16.0  0.095
2   Qn1 Quebec nonchilled  175   30.4  0.175
3   Qn1 Quebec nonchilled  250   34.8  0.250
4   Qn1 Quebec nonchilled  350   37.2  0.350
5   Qn1 Quebec nonchilled  500   35.3  0.500
6   Qn1 Quebec nonchilled  675   39.2  0.675

Working with Data - Data Manipulation - Summarise

  • group_by() and summarise() work together to aggregate variables
CO2 %>% group_by(Type) %>% summarise(avg_uptake = mean(uptake))
# A tibble: 2 x 2
         Type avg_uptake
       <fctr>      <dbl>
1      Quebec   33.54286
2 Mississippi   20.88333

Working with Data - Data Manipulation (dplyr) - Tips

  • In datasets with a lot of columns you can select all columns but selected ones by using the '-' operator select(CO2, -Plant)
  • When aggregating data you need an aggregate function such as mean, median, n (number of rows in a group), sum, etc.
  • If your data has missing values, you need to add the parameter na.rm = TRUE, this will skip any missing values.
  • When working with data, the data manipulation process often takes more time than the analysis.

Exercise 3

  • Review of some data manipulation functions in dplyr
  • Open ./exercises/intro/exercise_3.Rmd

Tydyverse

Further Learning - Beginner/Intermediate

  • Beginner/Intermediate Topics
    • User Defined Functions
    • Control Structures
    • Statistical Methods
    • Data Cleaning
    • Data Visualization Libraries (ggplot2, plotly)

Further Learning - Advanced

  • Advanced Topics
    • Deep Learning
    • Functional Programming
    • Object Oriented Programming
    • Debugging Tools
    • Performance

Hands-On Project

Hands-On Project

  • In this workshop we have been using the console but when working on a project you will generally use the editor so you can save your scripts. This helps anyone reproduce your findings.
  • When using a script, you must run your code either all at once (Run All) or Line-by-Line

Hands-On Project - Reading the Data

  • Download the data to your computer at http://bit.ly/r-hands-on-s2018
  • You can import the data to R either by code or by clicking Import Dataset on the Environment Tab
# You need to put the full path to the file in quotes or change the working directory [getwd(), setwd()]
Births <- read.csv("Births2015.csv")
Auto   <- read.csv("Auto.csv")

Hands-On Project - Analysis

  1. What is the name of the 1st car in the dataset?
  2. What is the total number of babies born in 2015?
  3. Make a boxplot of car mpg.
  4. Make a histogram of number of births.
  5. How many babies are born on Wednesdays?
  6. Which date had the least amount of babies born?
  7. Is there a relationship between the number of cylinders and mpg?
  8. Plot the average mpg for cars of each number of cylinders?

Hands-On Project - Answers

  1. head(Auto,1): chevrolet chevelle malibu
  2. sum(Births$births): 3978497
  3. boxplot(Auto$mpg)
  4. hist(Births$births)
  5. Births %>% group_by(wday) %>% summarise(sum=sum(births)) %>% arrange(sum): 638513
  6. arrange(Births, births): 12/25/2015
  7. cor(Auto$mpg, Auto$cylinders) or plot(Auto$cylinders, Auto$mpg)

Hands-On Project - Answer #8

x <- Auto %>% group_by(cylinders) %>% summarise(mean(mpg))
plot(x$cylinders, x$`mean(mpg)`)

plot of chunk unnamed-chunk-32

Hands-On Project - Answer #8 - 2

plot(x$cylinders, x$`mean(mpg)`, type='o', xlab="Cylinders", ylab="Average MPG", main="Cylinders vs. Average MPG") # Cleaner Version

plot of chunk unnamed-chunk-33

Resources