Lecture S2
Lecture S2
Oliver Hinder
12/23/2020
R and R studio
I Click Help in the menu bar then Cheetsheets then click the
cheatsheet you want to look at.
Action item: Explore R Markdown Cheat Sheet, RStudio IDE Cheat
Sheet and R Markdown Cheat Sheet
Interactive R
## [1] 8
Run a function
x = rnorm(20)
y = rexp(20)
plot(x,y)
2.0
1.5
y
1.0
0.5
0.0
−2 −1 0 1
x
What does a function do?
## [1] 5
Adding comments
# This is a comment in R.
# Remember use comments to document your code.
Some data types
## [1] FALSE
!FALSE # Not false?
## [1] TRUE
FALSE | TRUE # Either true or false?
## [1] TRUE
More data types
I Floats
x <- 1.0
# Double means a floating point number stored with 64 bits of pr
# https://en.wikipedia.org/wiki/Double-precision_floating-point_
typeof(x)
## [1] "double"
class(x)
## [1] "numeric"
y <- 1
class(y) # This is still a floating point number
## [1] "numeric"
I Integers
z <- 1L # Add L to specify that is an integer
class(z)
## [1] "integer"
More data types
I Characters
my_string <- "hello"
class(my_string)
## [1] "character"
print(my_string)
## [1] "hello"
I Dates
course_start <- as.Date("2020-01-18")
course_end <- as.Date("2020-05-01")
course_length <- course_end - course_start
print(course_length)
I Vectors (numbers)
I You can apply functions over entire vectors (vectorization)
a <- c(3, 5, 3, 7, 10)
b = c(1, 2, 3, 4, 5)
1/b
## [1] 8.8
var(a)
## [1] 8.8
More data structures
## [1] -5 -3 -1 1 3 5
seq(-5, 5, 2)
## [1] -5 -3 -1 1 3 5
seq(-5, 5, by=2)
## [1] -5 -3 -1 1 3 5
?seq
## [1] -2 -1 0 1 2 3 4
Matrices
## [,1] [,2]
## [1,] 1 4
## [2,] 2 5
## [3,] 3 6
More matrices
## [,1] [,2]
## [1,] "1" "dog"
## [2,] "2" "cat"
## [3,] "3" "bird"
## [4,] "4" "dog"
I Create matrix by binding rows together
mat3 <- rbind(c(1,2,4,5), c(6,7,0,4))
mat3
## [1] 3 2 2 1 0 -1
students$house
Dataframes from vectors