Learning Objectives This Week


In the Asynchronous Lecture


In the Synchronous Lecture


Tips

In this class, we’ll cover a lot of ground. Keep in mind that we’ll revisit these concepts again and again, so no worries if everything doesn’t sink in on your first pass.

If you have questions while watching the pre-recorded material, be sure to write them down and to bring them up during the synchronous portion of the lecture.




Asynchronous Materials


The following tabs contain pre-recorded lecture materials for class this week. Please review these materials prior to the synchronous lecture.

Total time: 1 hour, 28 minutes


_




Objects

Code from the video

# Object 1
x1 <- 4

# Object 2
x2 <- "This is a cat"

# Addition operator works for some classes, but not all
x1 + 1
x2 + 1

# See the class
class(x1)
class(x2)

# Coerce an object to be another class
as.character(x1)
as.numeric(x2)

# removing objects
rm(x2)



Data Structures

Code from the video

# Scalar data types
x1 <- 4
x2 <- "A"
x3 <- T

# Looking up the class
class(x3)

# Looking up the length of an object
length(x1)

# Make vector
v1 <- c(1,2,3,4)
length(v1)

# Only homogenous data types
v2 <- c("A",1,1.4,T)
v2



Indexing

Code from the video

# Scalar Data Types -------------------------------------------------------

    4           # Integers
    3.4         # Numeric (double/floats)
    TRUE        # Logical (Boolean)
    "A"         # Character
    factor("A") # Factor
 

# Data Structures ---------------------------------------------------------

    # Vector
    vec <- c("a","b","c","d")
    vec
  
    # List
    my_list <- list(A = 1:4, B = 1:10)
    my_list
    
    # Matrix
    my_mat <- matrix(0, ncol=2,nrow = 5)
    my_mat
    
    # Data Frame
    my_dat <- data.frame(A = 1:4, B = c("a","b","c","d"))
    my_dat

    

# Index -------------------------------------------------------------------

    vec <- c("a","b","c","d","e")
    vec    
    
    vec[-c(1,5,3)]
    vec[-3:-5]
    
    
    data$dist
    data[, 2]
    
    
    data[ 45:50 , c("speed","dist")]
    
    head(data,5)
    tail(data,5)



Operators

Code from the video

# Math Operators ----------------------------------------------------------

# + addition

    4 + 2
  
    # The addition operator is really function under the hood
    `+`(4,2) 
    
    # You cant' add all data types.
    "a" + 2
  
# - subtraction
    
    4 - 2

# / division
  
    4/5

# * multiplication
    
    4*5

# ^ Exponentiation
    
    2^3
  
    
# Built in Math Functions ---------------------------------
  
  # There are many statistical and mathematical functions in R. They operate the
  # same as any function and we'll see them in used in different ways in this
  # class.
  
  # log()
  # exp()
  # mean()
  # median()


# Logical Operators -------------------------------------------------------

  x <- 3
  y <- 4
  
  # Logical statements
  x == y      # equals to
  x != y      # does not equal
  x >= y      # greater than or equal to
  x <= y      # less than or equal to
  x > y       # greater than
  x < y       # less than
  
  # Compound logical statements
  
  # And operator: & -- if both left and right side of the statement are true,
  # then return true, else false
  x == 3 & y ==4
  x == 3 & y ==1
  
  
  # Or operator: | -- if one or the other side of the statement is true,
  # then return true, else false if both are false
  x == 3 | y ==1
  

# Vectorization -----------------------------------------------------------

  # Perform operations to all values to a vector simultaneously  
  x <- 1:10
  
  x + 5
  x * 100
  
  
  # Vectorize works for logical operators as well
  x > 5
  x != 5 & x > 3

  
# Subsetting --------------------------------------------------------------

  # We can use vectors of boolean values to subset vectors.
  x[x==5]
  x[x > 5]
  x[x != 5 & x > 3] # Returns a subset
  x[x != 5 | x > 3] # Returns everything!
  
  

# Misc --------------------------------------------------------------------

  
  # Note that we can store vector of logical values to it's own object
  b1 <- x != 5 & x > 3
  # And use it later
  x[b1]
  
  # Also note that we can coerce a logical vector to be numeric, which turns it
  # into an indicator (0,1) variable
  as.numeric(b1)



Functions & Packages

Code from the video

# What are functions? ------------------------

    # We've already run into a number of functions
    c() # generate a vector
    as.character() # coerce to character class
    data.frame() # create a data frame
    # etc. ...
    
    
    # Functions are denoted by a word and parentheses
    # "word" with () = word()
    
    # Functions take arguments
    # word(arg1 = 1,arg2 = 2)
    
    # And generate some kind of output
    # output <- word(arg1 = 1,arg2 = 2)
    
    # Functions are objects like any other object in R. 
    
    # Functions are ways to house code. 


# Base Functions  ------------------------
    
    # Base functions come pre-installed on R. When you turn R on, you have
    # access to all base functions,


# Function Documentation  ------------------------

    # We can use `?` to look at a functions documentation
    ?mean()

    
# Why we import packages?  ------------------------

    # We import packages because we get additional functionality that is not
    # native to the R programming environment

# Installing packages  ------------------------
    
    # We can install packages using the `install.packages()` function
    install.packages("ggplot") # We simply need to provide the name of the function. 
    

# Importing Packages ------------------------------------------------------

    # Once a package is installed, we can now import the package and all it's
    # functions using `library()` or `require()`
    library(ggplot2)
    require(ggplot2)
    
    

# Referencing Package Functions without installing the pacakge ------------

    # We can reference package functions without install the package using `::`
    # operator. Specifically, we use <package_name>::<function_name>
    ggplot2::ggplot()



Practice


These exercises are designed to help you reinforce your grasp of the concepts covered in the asynchronous lecture material.

Feedback

The following survey asks you quick questions regarding the usefulness of the asynchronous lecture materials. Feedback will be used to modify aspect of the asynchronous materials moving forward.

 

The following materials were generated for students enrolled in PPOL670. Please do not distribute without permission.

ed769@georgetown.edu | www.ericdunford.com