Lecture 1
Lecture 1
• R is a programming language
• One can use this language in two different ways:
– directly through the R console
– and using the RStudio development
environment.
• R is open source.
• R is flexible.
R Console
Computations Objects
• Addition: 7+8 • Named data
structure that allows
• Subtraction: 7-6
us to reference that
• Multiplication: 2*7 data structure
• Division: 15/3 • Commonly
• Exponentiation: 8^7 called variables.
• Modulo: 100 %% 5
Rstudio environment
Panes
• Vectors are 1-dimensional data • The class() function returns the data
structures that can store multiple data structure of an object.
values of the same type (e.g. character,
boolean, or numeric). • Another way to create a vector is with
• A vector is a sequence of values, all of the rep() or seq() functions
the same type
•x <- c(7, 8, 10, 45) •rep(0, 5)
#> [1] 0 0 0 0 0
x
•rep("Monday", 4)
[1] 7 8 10 45 #> [1] "Monday" "Monday" "Monday" "Monday"
•is.vector(x) •seq(1, 5, 1)
#> [1] 1 2 3 4 5
[1] TRUE
•seq(0, -10, -2)
•c() function returns a vector containing all
#> [1] 0 -2 -4 -6 -8 -10
its arguments in order
Indexing Vectors
•Once we have a vector, we may want to access certain values
stored in that vector.
•To do so, we index the vector using the position of each value
– x[1] is the first element,
– x[4] is the 4th element
– x[-4] is a vector containing all but the fourth element
• Vector arithmetic
– Operators apply to vectors “pairwise” or •Pairwise comparisons:
“elementwise”
•x <- c(7, 8, 10, 45)
•x > 9
•y <- c(-7, -8, -10, -45) [1] FALSE FALSE TRUE TRUE
• x+y
•Note: returns Boolean vector
[1] 0 0 0 0
•x*y •Boolean operators work
[1] -49 -64 -100 -2025 elementwise:
• Recycling
•(x > 9) & (x < 20)
– repeat elements in shorter vector when
combined with longer [1] FALSE FALSE TRUE FALSE
•x + c(-7,-8)
[1] 0 0 3 37
•x^c(1,0,-1,0.5)
Functions on Vectors
mean(), median(), sd(), var(), max(), min(), length(), sum():
return single numbers
sort() returns a new vector
hist() takes a vector of numbers and produces a histogram, a
highly structured object, with the side-effect of making a plot
Similarly ecdf() produces a cumulative-density-function object
summary() gives a five-number summary of numerical
vectors
any() and all() are useful on Boolean vectors
Factors
• In R, a matrix is a specialization of a
• Factory makes cars 2D array
and trucks, using > factory <-
labor and steel matrix(c(40,1,60,3),nrow=2)
> is.array(factory)
– a car takes 40 [1] TRUE
hours of labor and 1 > is.matrix(factory)
[1] TRUE
ton of steel • could also specify ncol, and/or
– a truck takes 60 byrow=TRUE to by rows.
• Element-wise operations with the
hours and 3 tons of usual arithmetic and comparison
steel operators (e.g., factory/3)
Matrix Multiplication
• Gets a special operator
> six.sevens <- matrix(rep(7,6),ncol=3)
> six.sevens
[,1] [,2] [,3]
[1,] 7 7 7
[2,] 7 7 7
> factory %*% six.sevens # [2x2] * [2x3]
[,1] [,2] [,3]
[1,] 700 700 700
[2,] 28 28 28
• What happens if you try six.sevens % * % factory?
Multiplication of matrices
and vectors
• Numeric vectors can act like proper vectors:
> output <- c(10,20)
> factory %*% output
[,1]
[1,] 1600
[2,] 70
> output %*% factory
[,1] [,2]
[1,] 420 660
• R silently casts the vector as either a row or a column matrix
Matrix Operators
• Transpose:
> t(factory)
[,1] [,2]
[1,] 40 1
[2,] 60 3
• Determinant:
> det(factory)
[1] 60
• Diagonoal: The diag() function can extract the diagonal entries of a
matrix:
> diag(factory)
[1] 40 3
Creating a diagonal of Identity
Matrix
> diag(c(3,4)) > diag(3)
[,1] [,2] [,1] [,2] [,3]
[1,] 3 0
[1,] 1 0 0
[2,] 0 4
[2,] 0 1 0
• > diag(2) [3,] 0 0 1
[,1] [,2]
[1,] 1 0
[2,] 0 1
Inverting Matrix
> solve(factory)
[,1] [,2]
[1,] 0.05000000 -1.0000000
[2,] -0.01666667 0.6666667
> factory %*% solve(factory)
[,1] [,2]
[1,] 1 0
[2,] 0 1
Naming Matrix