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)]