Utility Flashcards
Which function calculates the absolute value of a vector of numerical values, element-wise?
abs()
Ex: abs(my_vector)
Which function rounds each element of a vector to the nearest integer value?
round()
Ex: round(my_vector)
Which function calculates the arithmetic mean of a vector of numeric values?
mean()
Ex: mean(my_vector)
Which function creates a series of numbers, and what are the inputs to this function?
seq()
The seq function generates a sequence of numbers from x to y, incrementing or decrementing by z.
Ex: seq(x, y, by = z)
Which function replicates its input, and what are its inputs?
rep()
The first input to rep is the vector or list to be replicated. The times input repeats the input vector a specified number of times. The each input repeats each element a specified number of times.
Ex: rep(my_vector, times = 10)
How can you reverse the order of the sort() function?
By setting decreasing = TRUE
What function can you use to check the type of your data structure?
is.___
Ex: is.list(my_object), is.vector(my_object), etc.
What function can you use to attempt to transform one data type into another?
as.___
Ex: as.list(my_vector), etc.
Which function attempts to convert a list to a vector?
unlist()
Ex: unlist(my_list)
Which function reverses the order of the elements in a list?
rev()
Ex: rev(my_list)
Which function adds the first inputted list to the end of the second inputted list?
append()
Ex: append(my_list1, my_list2)
Which function checks if a given pattern exists in each string in a vector?
grepl()
Ex: grepl(pattern = “a”, x = my_vector)
How can you use grepl to check if a pattern exists at the beginning or end of a string?
Include a ^ at the beginning of the pattern or a $ at the end.
Ex: grepl(pattern = “^a”, x = my_vector)
Ex: grepl(pattern = “a$”, x = my_vector)
Which function returns a vector of indices whose elements contained the inputted pattern?
grep()
Ex: grep(pattern =”a”, x = my_vector)
Which function finds and replaces every instance of a certain pattern in a vector of strings?
sub()
Ex: sub(pattern = “a”, replacement = “b”, x = my_vector)
What is the difference between the sub and gsub functions?
While the sub function only replaces the first match in each string, gsub replaces every match.
How can you give multiple options inside grep, grepl, sub, or gsub?
Using the | operator.
Ex: gsub(pattern = “a|i”, replacement = “b”, x = my_vector)