Code
xs <- 2 # Make your first scalar
xs # Print the scalar
## [1] 2In R: scalars, vectors, and matrices are different kinds of “objects”.
These objects are used extensively in data analysis
Vectors are probably your most common object in R, but we will start with scalars.
Make your first scalar
xs <- 2 # Make your first scalar
xs # Print the scalar
## [1] 2Perform simple calculations and see how R is doing the math for you
xs + 2
## [1] 4
xs*2 # Perform and print a simple calculation
## [1] 4
(xs+1)^2 # Perform and print a simple calculation
## [1] 9
xs + NA # often used for missing values
## [1] NANow change xs, predict what will happen, then re-run the code.
Make your first vector
x <- c(0,1,3,10,6) # Your First Vector
x # Print the vector
## [1] 0 1 3 10 6
x[2] # Print the 2nd Element; 1
## [1] 1
x+2 # Print simple calculation; 2,3,5,8,12
## [1] 2 3 5 12 8
x*2
## [1] 0 2 6 20 12
x^2
## [1] 0 1 9 100 36Apply mathematical calculations elementwise
x+x
## [1] 0 2 6 20 12
x*x
## [1] 0 1 9 100 36
x^x
## [1] 1.0000e+00 1.0000e+00 2.7000e+01 1.0000e+10 4.6656e+04In R, scalars are treated as a vector with one element.
c(1)
## [1] 1Sometimes, we will use vectors that are entirely ordered.
1:7
## [1] 1 2 3 4 5 6 7
seq(0,1,by=.1)
## [1] 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0
# Ordering data
sort(x)
## [1] 0 1 3 6 10
x[order(x)]
## [1] 0 1 3 6 10Matrices are also common objects
x1 <- c(1,4,9)
x2 <- c(3,0,2)
x_mat <- rbind(x1, x2)
x_mat # Print full matrix
## [,1] [,2] [,3]
## x1 1 4 9
## x2 3 0 2
x_mat[2,] # Print Second Row
## [1] 3 0 2
x_mat[,2] # Print Second Column
## x1 x2
## 4 0
x_mat[2,2] # Print Element in Second Column and Second Row
## x2
## 0There are elementwise calculations
x_mat+2
## [,1] [,2] [,3]
## x1 3 6 11
## x2 5 2 4
x_mat*2
## [,1] [,2] [,3]
## x1 2 8 18
## x2 6 0 4
x_mat^2
## [,1] [,2] [,3]
## x1 1 16 81
## x2 9 0 4
x_mat + x_mat
## [,1] [,2] [,3]
## x1 2 8 18
## x2 6 0 4
x_mat*x_mat #NOT classical matrix multiplication
## [,1] [,2] [,3]
## x1 1 16 81
## x2 9 0 4
x_mat^x_mat
## [,1] [,2] [,3]
## x1 1 256 387420489
## x2 27 1 4And you can also use matrix algebra
x_mat1 <- matrix(2:7,2,3)
x_mat1
## [,1] [,2] [,3]
## [1,] 2 4 6
## [2,] 3 5 7
x_mat2 <- matrix(4:-1,2,3)
x_mat2
## [,1] [,2] [,3]
## [1,] 4 2 0
## [2,] 3 1 -1
tcrossprod(x_mat1, x_mat2) #x_mat1 %*% t(x_mat2)
## [,1] [,2]
## [1,] 16 4
## [2,] 22 7
crossprod(x_mat1, x_mat2)
## [,1] [,2] [,3]
## [1,] 17 7 -3
## [2,] 31 13 -5
## [3,] 45 19 -7Functions are applied to objects
# Define a function that adds two to any vector
add_two <- function(input_vector) { #input_vector is a placeholder
output_vector <- input_vector + 2 # new object defined locally
return(output_vector) # return new object
}
# Apply that function to a vector
x <- c(0,1,3,10,6)
add_two(input_vector=x) #same as add_two(x)
## [1] 2 3 5 12 8Common mistakes:
print(output_vector)
# This is not available globally
# Double check your spelling
x < - add_two(input_vector=X)
# Seeing "+" in the bottom console
# often means you forgot to close the function with "}"
# press "Escape" and try again
add_two <- function(input_vector) {
output_vector <- input_vector + 2
return(output_vector)
x <- c(0,1,3,10,6)
add_two(x)There are many different functions
add_vec <- function(input_vector1, input_vector2) {
output_vector <- input_vector1 + input_vector2
return(output_vector)
}
add_vec(x,3)
## [1] 3 4 6 13 9
add_vec(x,x)
## [1] 0 2 6 20 12
sum_squared <- function(x1, x2) {
y <- (x1 + x2)^2
return(y)
}
sum_squared(1, 3)
## [1] 16
sum_squared(x, 2)
## [1] 4 9 25 144 64
sum_squared(x, NA)
## [1] NA NA NA NA NA
sum_squared(x, x)
## [1] 0 4 36 400 144
sum_squared(x, 2*x)
## [1] 0 9 81 900 324Functions can take functions as arguments. Note that a statistic is defined as a function of data.
statistic <- function(x, f){
y <- f(x)
return(y)
}
statistic(x, sum)
## [1] 20There are many possible functions you can make and use. More complicated functions often have defaults.
fun_of_seq <- function(f, constant=2){
x1 <- seq(1,3, length.out=12)
x2 <- x1+constant
x <- cbind(x1,x2)
y <- f(x)
return(y)
}
fun_of_seq(sum)
## [1] 72
fun_of_seq(sum, 3)
## [1] 84
fun_of_seq(prod)
## [1] 30799645993
fun_of_seq(prod, 3)
## [1] 473621744988You can also apply functions to matrices
sum_squared(x_mat, x_mat)
## [,1] [,2] [,3]
## x1 4 64 324
## x2 36 0 16
# Apply function to each matrix row
y <- apply(x_mat, 1, sum)^2
# ?apply #checks the function detailsApplying the same function over and over again
#Create empty vector
exp_vector <- vector(length=3)
#Fill empty vector
for(i in 1:3){
exp_vector[i] <- exp(i)
}
# Compare
exp_vector
## [1] 2.718282 7.389056 20.085537
c( exp(1), exp(2), exp(3))
## [1] 2.718282 7.389056 20.085537A more complicated example
complicated_fun <- function(i, j=0){
x <- i^(i-1)
y <- x + mean( j:i )
z <- log(y)/i
return(z)
}
complicated_vector <- vector(length=10)
for(i in 1:10){
complicated_vector[i] <- complicated_fun(i)
}A recursive example
x <- vector(length=4)
x[1] <- 1
for(i in 2:4){
x[i] <- (x[i-1]+1)^2
}
x
## [1] 1 4 25 676TRUE/FALSE
x <- c(1,2,3,NA)
x > 2
## [1] FALSE FALSE TRUE NA
x==2
## [1] FALSE TRUE FALSE NA
any(x==2)
## [1] TRUE
all(x==2)
## [1] FALSE
2 %in% x
## [1] TRUE
2==TRUE
## [1] FALSE
2==FALSE
## [1] FALSE
is.numeric(x)
## [1] TRUE
is.na(x)
## [1] FALSE FALSE FALSE TRUEThe “&” and “|” commands are logical calculations that compare vectors to the left and right.
x <- 1:3
(x >= 1) & (x < 2)
## [1] TRUE FALSE FALSE
(x >= 1) | (x < 2)
## [1] TRUE TRUE TRUE
if( all(x >= 1) ){
print("ok")
} else {
print("not ok")
}
## [1] "ok"
logic_fun <- function(x){
if( all(x >= 1) ){
print("ok")
} else {
print("not ok")
}
}
logic_fun(1:3)
## [1] "ok"
logic_fun(0:2)
## [1] "not ok"Factorials are used for counting the different ways of arranging \(n\) distinct objects into a sequence: \(n!=1\times2\times3 ... (n-2)\times(n-1)\times n\).
factorial(3)
## [1] 6The binomial coefficient \(\tbinom {n}{k}\) counts the subsets of \(k\) elements from a set with \(n\) elements.
#Ways to draw k=2 from a set with n=4
choose(4,2)
## [1] 6The exponential function is \(e^{x}=1+x/1+2^2/2+x^3/6....=\sum_{k=0}^{\infty} x^k/k!\) and the \(log\) function is it’s inverse.
x <- exp(4)
x
## [1] 54.59815
y <- log(x)
y
## [1] 4