R has range of operators including most basic math operators.
# math operators
2 + 3
## [1] 5
4 - 2
## [1] 2
6 * 7
## [1] 42
7 / 3
## [1] 2.333333
4 ^ 2
## [1] 16
5 %% 3
## [1] 2
# R also has logical operators
5 == 4
## [1] FALSE
5 != 4
## [1] TRUE
5 < 4
## [1] FALSE
5 > 4
## [1] TRUE
5 <= 4
## [1] FALSE
5 >= 4
## [1] TRUE
5 == c(4, 5, 6)
## [1] FALSE TRUE FALSE
5 %in% c(4, 5, 6)
## [1] TRUE
# we create variables using the assignment operator
x <- 5
5 -> x
# the equal sign can do this too but it is bad form
x = 5
z <- 5
x == z
## [1] TRUE
R has four primary data types that you will interact with numeric, character, logical, and factor.
# make a numeric vector
scores <- c(68, 96, 59, 88, 91)
scores
## [1] 68 96 59 88 91
# make a character vector
students <- c("Heath", "Jen", "LT", "Abbi", "Rogue")
students
## [1] "Heath" "Jen" "LT" "Abbi" "Rogue"
# make a logical vector
passed <- c(F, T, FALSE, TRUE, TRUE)
passed
## [1] FALSE TRUE FALSE TRUE TRUE
# make a factor vector
status <- factor(x=c("PhD", "PhD", "PreBac", "PreBac", "PreBac"))
status
## [1] PhD PhD PreBac PreBac PreBac
## Levels: PhD PreBac
You can store data in four primary structures: vector, matrix, dataframe, and list.
# making a vector (limited to one data type)
x <- c(23, 56)
x
## [1] 23 56
x <- c(34, "Heath")
x
## [1] "34" "Heath"
In the second example R converts the number and stores it as a character becuase all elements of the vector must have the same data type.
# making a matrix (limited to one data type)
x <- matrix(data = 1:12, nrow = 6, ncol = 2)
x
## [,1] [,2]
## [1,] 1 7
## [2,] 2 8
## [3,] 3 9
## [4,] 4 10
## [5,] 5 11
## [6,] 6 12
x <- matrix(data = c(1:11, T), nrow = 6, ncol = 2)
x
## [,1] [,2]
## [1,] 1 7
## [2,] 2 8
## [3,] 3 9
## [4,] 4 10
## [5,] 5 11
## [6,] 6 1
In the second example R converst the T value to a one since like the vector the matrix is limited to one data type.
# making a dataframe (each column must have only one data type)
x <- data.frame(students, scores, passed, status)
x
## students scores passed status
## 1 Heath 68 FALSE PhD
## 2 Jen 96 TRUE PhD
## 3 LT 59 FALSE PreBac
## 4 Abbi 88 TRUE PreBac
## 5 Rogue 91 TRUE PreBac
# to make a list we use the function list.
mylist <- list(4, 6, 3)
mylist
## [[1]]
## [1] 4
##
## [[2]]
## [1] 6
##
## [[3]]
## [1] 3
# unlike a vector a list can hold many different types of data
mylist <- list(c(4, 5), "heath", matrix(c(T, F, F, T), 2, 2))
mylist
## [[1]]
## [1] 4 5
##
## [[2]]
## [1] "heath"
##
## [[3]]
## [,1] [,2]
## [1,] TRUE FALSE
## [2,] FALSE TRUE
# here we see a list with the first element having a numeric vector of length two, a second element of character vector of length one, and a final element with a logical matrix in it.
You can access data in several ways. One of the most common is to use square braces.
students
## [1] "Heath" "Jen" "LT" "Abbi" "Rogue"
If we want to access parts of this vector we can use brackets with either single values or vectors that specify what parts of the vector we want.
# get a single element out
students[3]
## [1] "LT"
# get two elements out
students[1:2]
## [1] "Heath" "Jen"
students[c(1,4)]
## [1] "Heath" "Abbi"
# use a logical vector to extract certain values
students[passed]
## [1] "Jen" "Abbi" "Rogue"
if we want to access parts of matrix or a data frame we need to specify first the rows we want followed by the columns that we want. Both of these values will be in square brackets and will be separated by a comma.
mymatrix <- matrix(data = 1:40, nrow = 10, ncol = 4)
mymatrix[1, 3]
## [1] 21
mymatrix[3:7, 2]
## [1] 13 14 15 16 17
mymatrix[3, c(T,F,T,F)]
## [1] 3 23
if are working with a dataframe we can also use the column names and a $ sign to access data lets look back at the data frame x that we made earlier to see what that looks like
x$students
## [1] "Heath" "Jen" "LT" "Abbi" "Rogue"
x$students[c(1,3)]
## [1] "Heath" "LT"
Control structures are where a lot of the power comes from in R or any other programming language. Today we will look at for, while, and if.
# The function for will repeat a block of code for you many times
for(i in 1:10){
print(i)
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## [1] 6
## [1] 7
## [1] 8
## [1] 9
## [1] 10
# If will run code but only if the statement that it evaluates is true
x <- FALSE
if(x){
print("statement one")
}
x <- TRUE
if(x){
print("statement two")
}
## [1] "statement two"
# and we see that only statement two is printed
# While will run code until the statment it is given changes to FALSE
working <- TRUE
tries <- 1
while(working){
# roll six dice
rolls <- sample(x = 1:6, size = 6, replace = T)
if(sum(rolls) == 36){
working <- F
}
tries <- tries + 1
}
print(paste("It took me", tries, "tries to roll all sixes."))
## [1] "It took me 6185 tries to roll all sixes."
The code below does something. Can you determine what the script will do?
results <- c()
for(i in 1:100){
tries <- 0
working <- T
while(working){
diceroll <- sample(x=1:6, size = 4, replace = T)
dicecheck <- sum(diceroll) == 24
tries <- tries + 1
if(dicecheck){
results[i] <- tries
working <- F
}
}
}
hist(results)
abline(v = mean(results), col = "red", lwd = 3)