Sequences, Vectors, Matrices, and Data Frames Flashcards
Create a sequence of numbers from a to b
a:b or seq(a, b)
Create a sequence of numbers from 1 to 10, incremented by 0.5
seq(1, 10, by=0.5)
Get the length of a sequence
length(sequence)
Make a vector with 10 repetitions of the vector (0, 1, 2)
rep(c(0, 1, 2), times = 10)
Make a vector that repeats 10 zeros, then 10 ones, then 10 twos
rep(c(0, 1, 2), each=10)
What are the 2 types of vectors?
atomic vectors = contain one data type lists = can contain multiple data types
Conditional expressions: symbol for OR symbol for AND
or: A | B
and: A & B
Note that AND operators get evaluated before OR operators
Join together the elements in the following vector: c(“My”, “name”, “is”)
paste(c(“My”, “name”, “is”), collapse= “ “) #collapse tells R that want spaces between characters when join together
Add an element to this vector: my_char = c(“My”, “name”, “is”)
c(my_char, “Estefy”)
Join “Hello” and “world” with a space between words
paste(“Hello”, “world”, sep = “ “)
Generate the following output: A-1, B-2, C-3 Use vectors 1:3 and c(“A”, “B”, “C”)
paste(c(“A”, “B”, “C”), 1:3)
What do mathematical operations involving NA yield?
NA
Which function tells you whether each element of a vector is NA?
is.na(dataset) #It generates a vector of booleans with TRUE for missing values
Count the number of missing values in my_na = is.na(dataset)
sum(my_na) This will add up the boolean array where TRUE = 1 and FALSE = 0, giving number of missing values
Make a vector of all non-missing values in vector x
x[!is.na(x)]
Make a vector with the 3rd and 5th elements of vector x
Make a vector with all the elements of x except for the 3rd and 5th elements
x[c(3, 5)]
x[c(-3, -5)] or x[-c(3, 5)]
Create a numeric vector with 3 named elements:
foo=11
bar =2
norf= NA
Print the names of the vector
vect <- c(foo = 11, bar = 2, norf = NA)
names(vect)
Output: “foo” “bar” “norf”
Add names to vector = c(1, 2, 3)
names: “hey”, “there”, “you”
names(vector) <- c(“hey”, “there”, “you”)
How do you check if two vectors are the same?
identical(vector1, vector2)
Output will be true or false
True or False
Matrices can contain many different classes of data
data frames can contain many different classes of data
FALSE: matrices can only contain a single class of data
TRUE: data frames can contain many different classes of data
add dimension 4, 5 to a vector x
dim(x) <- c(4, 5)
Create a matrix with the numbers from 1 to 20 with 4 rows and 5 columns
Add row names to the matrix: “one”, “two”, “three”, “four”
Create a data frame with row names and matrix
Add column names to the data frame: “One”, “Two”, “Three”, “Four”, “Five”
my_matrix <- matrix(1:20, nrow=4, ncol=5)
names = c(“one”, “two”, “three”, “four”)
cbind(names, my_matrix)
Note that this will coerce all numbers into characters
my_data <- data.frame(names, my_matrix)
cnames <- c(“One”, “Two”, “Three”, “Four”, “Five”)
colnames(my_data) <- cnames
TRUE function
exclusive OR function
xor() returns true if one argument is TRUE and the other one is FALSE, otherwise, it will return FALSE
isTrue(expression)
xor(expression1, expression2)
Add the rows of my_matrix
Add the columns of my_matrix
rowSums(my_matrix)
colSums(my_matrix)
add matrix2 to matrix 1 as columns
add matrix2 as rows to matrix1
cbind(matrix1, matrix2)
rbind(matrix1, matrix2)