Introduction to Statistical Programming with R Discussion Section A,B
Time: Tuesday, 8:00 am to 9:50 am
TA: Feng Gao (Pronunciation in English: Fung Gao) Email: f.gao@ucla.edu
Office Hour
Wednesday 8:30 a.m. – 9:30 a.m. Friday 8:30 a.m. – 9:30 a.m.
Introduction
Goal of the Class:
Learn basic R programming ideas and skills for further study in Statistics.
Goal of the Discussion and Office Hour:
Resolve the questions.
Note:
¡ñ I am here to help you instead of torturing you.
¡ñ Think beyond the code itself.
Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime. – Lao Tzu
Programming Language: An Analogy Could you please …..?
Codes
Install R and RStudio
R:
https://www.r-project.org/
RStudio:
https://rstudio.com/
Install the Required Packages
Link:
https://github.com/elmstedt/UCLAstats20/
Command to run in RStudio Console:
install.packages(“devtools”)
library(devtools)
install_github(“elmstedt/UCLAstats20”)
Get Access to the Interactive Notes
Every time you relaunch R:
library(UCLAStats20 )
notes(#)
Code Style https://style.tidyverse.org/index.html
10/13 Discussion
Vectors in R
TA: Feng Gao Email: f.gao@ucla.edu
Clean Up
¡ñ Any questions for the materials in the last homework?
¡ñ Any suggestions?
¡ñ My suggestions:
¡ð Try to code and run it first. Don¡¯t just keep the idea in your mind. Write down the code first and
see what will happen.
¡ð Make sure you leverage all the available materials, e.g. CCLE, interactive notes, Campuswire.
¡ð Ask question after you tried. Don¡¯t be hand-waving.
Vectors in R
Everything in R is vector!
Differences between Vector and Set
Vector:
Order.
Each element is not necessary to be unique.
Set:
No order.
Elements have to be unique.
Create Vectors and Mode Hierarchy
Create a Vector:
c()
Mode Hierarchy:
logical < numeric <
character
More on Mode Hierarchy
¡ñ Combining logical and numeric vectors will result in a numeric vector.
¡ñ Combining numeric and character vectors will result in a character vector.
¡ñ Combining logical and character vectors will result in a character vector.
¡ñ Combining logical, numeric, and character vectors will result in a character
vector.
Some Examples
fib <- c(1, 1, 2, 3, 5, 8, 13)
parks <- c("Leslie", "April", "Ron", "Tom", "Donna",
"Jerry")
true_dat <- c(TRUE, FALSE, TRUE, T, F)
¡ñ mode(c(fib, parks))
¡ñ mode(c(fib, true_dat))
¡ñ mode(c(parks, true_dat))
¡ñ mode(c(fib, parks, true_dat))
Sequences and Repeated Patterns
Create a sequence:
seq(from, to, by)
seq(from, to, length.out)
Repeat:
rep(x, times = 1, length.out = NA, each = 1)
rep(c(1,2,3), 2)
The Colon :
Functions the same as Seq():
-2:5 # same as seq(-2, 5)
pi:10 # same as seq(pi, 10)
What are the differences?:
1:n - 1
1:(n - 1)
The seq_len() and seq_along() Function
seq_len():
The seq_len() function inputs a single length.out argument and generates the sequence of integers
seq_along():
The seq_along() function inputs a single along.with argument and generates the sequence of integers 1, 2, ..., length(along.with). In conclusion, it returns index(s).
Function rep()
The rep() function creates a vector of repeated values. The first argument, generically called x, is the vector of values we want to repeat. The second argument is the times argument that specifies how many times we want to repeat the values in the x vector.
What are the returns and why?:
rep(seq(2, 20, by = 2), 2)
rep(seq(2, 20, by = 2), rep(2, 10))
Extracting and Assigning Vector Elements
Extracting, what the the returns?:
running_times[5]
running_times[c(3, 7)]
running_times[4:8]
running_times[-4]
running_times[-c(1, 5)]
running_times[-(1:4)]
running_times[c(-1, 3)]
running_times[1.9]
running_times[-1.9]
running_times[0.5]
Extracting and Assigning Vector Elements
Assigning, what the the returns?:
running_times[4] <- 43
running_times[9:10] <- c(42, 37)
bad[1:2] <- c(4, 8)
Vector Arithmetic
What are the values?:
x <- c(1, 3, 5)
y <- c(2, 4, 3)
x+ y x* y
x^y
Vector Arithmetic
Recycling:
When applying arithmetic operations to two vectors of different lengths, R will automatically recycle, or repeat, the shorter vector until it is long enough to match the longer vector.
c(1, 3, 5) + c(5, 7, 0, 2, 9, 11)
c(1, 3, 5) + 5
c(1, 3, 5) + c(5, 7, 0, 2, 9)
Vectorization
Suppose we have a function that we want to apply to all elements of a vector. In many cases, functions in R are vectorized, which means that applying a function to a vector will automatically apply the function to each individual element in the vector.
The vapply() Function
Built-in Functions
Special Values
NA:
NULL:
NaN:
0/ 0
Inf:
1 /0
running_times <- c(running_times[1:5], NA, running_times[6:10])
mean(running_times)
nada <- NULL
mode(nada)
10/20 Discussion
Logic Expression & Control Flow
TA: Feng Gao Email: f.gao@ucla.edu
Clean Up
¡ñ Any questions for the materials in the last homework?
¡ñ Any suggestions?
¡ñ I don¡¯t have the solutions either.
¡ñ My suggestions
¡ð Don¡¯t focus on interpreting the hints. It is not helpful beyond the HW.
¡ð Please concentrate on the problem itself.
¡ð Using the learned materials in the class as the building blocks.
Relational Operators
Binary Operators that returns TRUE or FALSE
Cautions on == and !=
49 * (4 / 49) == 4 # Is 49 * (4 / 49) exactly equal to 4?
Relational Operators: Vectorization
c(3, 8) >= 3
c(1, 4, 9) == 9
c(3, 8) < c(1, 4)
c(1, 4, 9, 3, 8) > c(5, 6, 7)
The any(), all(), and identical() Functions
any() all()
identical(c(1,2,3,4), 1:4)
Special Values
NA: Not Available
c(7, NA, 4) > 6
NULL: Nothing
NaN: Not a Number (implies an illegal math expression)
c(1, 4, 9) <= NaN
Inf: everything larger than a large enough threshold
c(TRUE, FALSE) > NULL
c(1,2) > NULL
c(1, 4, Inf) < Inf
exp(1000) == Inf
Logical Indexing
Expression:
logical_index <- (running_times %% 2) == 0
Extracting elements:
running_times[running_times > 40]
which() function: returns indices whose corresponding logical expression is TRUE
which(running_times >= 50)
Boolean Operators
&: AND
some_nums > 3 & some_nums < 7
|: OR
some_nums < 3 | some_nums > 7
!: NOT
!(some_nums < 3)
The && and || Operators
x <- -5
x < 0 | is.na(sqrt(x) > 2)
x < 0 || is.na(sqrt(x) > 2)
x < 0 && is.na(sqrt(x) > 2)
x < 0 & is.na(sqrt(x) > 2)
From left to right. Not necessary to compute all of them
Control Flow
Loop:
for(iteratable){
}
While(condition){
}
repeat{
}
Condition Statement:
if(condition){
}
else{
}
Some Questions
Can you implement:
max() and min() with if{}else{}
When to use:
for(iteratable){
}
While(condition){
}
repeat{
}
10/27 Discussion
Review Section
TA: Feng Gao Email: f.gao@ucla.edu
Chapter 1 Key Points:
Basic Arithmetic: +, -, *, /, ^
Modular Arithmetic: %%, %/%
Order of Operations: See HW3, Question 6
Object Assignment: What is a legal name of an object?; Masking; Built-in Objects
Function: Syntax; What will a function return by default?; How to create/call a function?; local environment, global environment, parent environment, mask?
HW1 Intermediate Questions
Chapter 2 Key Points:
Vector: Basic data types; Mode hierarchy and Coercion; Length; c() function Import functions: seq() function, rep() function, seq_len() function, seq_along()
function.
Extract, Assign Vector Elements: Negative index; Multiple indices; Fractional index; Blank index;
Vector Arithmetic: Recycling; Vectorization; vapply() function Built-in Functions: Chapter 2 notes, Section 6.
Special Values: See discussion slides; Floating point representation
HW2 Intermediate Questions
Chapter 3 Key Points:
Relational Operators: <, <=, >, >=, ==, != ; See notes for detail. Vectorized; Recycling; any(), all, identical() function; Special values Logical Indexing: Syntax; Extract elements; which() function
Boolean Operators: &, |, !, &&, || ; Differences between &, | and &&, ||
Chapter 4 Key Points
Loop:
Syntax of Loops. for, while, repeat. How to write loops?
When to use?
HW3 Intermediate Questions
Chapter 5 Key Points:
Matrix: matrix() function; Matrix indexing; Dimension;
cbind() function;rbind() function
Naming row and columns: colnames(), rownames() function; dimnames() function
Extracting data from 2-D matrix: Single element; Entire row; Entire column; Remove element, row, column
Logical Indices
Entrywise Arithmetic; Matrix Arithmetic
Diagonal matrix:diag() function; Inverse matrix solve() function; apply() function
Tips: How to write/implement a function
1. Clarify the INPUT and the OUTPUT.
2. Think through the algorithm.
2.1. Any Constraints?
2.2. What is the Time Complexity? (How many times of loops) 2.3. Logic
2.4 Corner Cases
3. Design TEST CASES (Very important).