Utility Flashcards

1
Q

Which function calculates the absolute value of a vector of numerical values, element-wise?

A

abs()

Ex: abs(my_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which function rounds each element of a vector to the nearest integer value?

A

round()

Ex: round(my_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which function calculates the arithmetic mean of a vector of numeric values?

A

mean()

Ex: mean(my_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which function creates a series of numbers, and what are the inputs to this function?

A

seq()

The seq function generates a sequence of numbers from x to y, incrementing or decrementing by z.

Ex: seq(x, y, by = z)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which function replicates its input, and what are its inputs?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can you reverse the order of the sort() function?

A

By setting decreasing = TRUE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What function can you use to check the type of your data structure?

A

is.___

Ex: is.list(my_object), is.vector(my_object), etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What function can you use to attempt to transform one data type into another?

A

as.___

Ex: as.list(my_vector), etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Which function attempts to convert a list to a vector?

A

unlist()

Ex: unlist(my_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Which function reverses the order of the elements in a list?

A

rev()

Ex: rev(my_list)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Which function adds the first inputted list to the end of the second inputted list?

A

append()

Ex: append(my_list1, my_list2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Which function checks if a given pattern exists in each string in a vector?

A

grepl()

Ex: grepl(pattern = “a”, x = my_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you use grepl to check if a pattern exists at the beginning or end of a string?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Which function returns a vector of indices whose elements contained the inputted pattern?

A

grep()

Ex: grep(pattern =”a”, x = my_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Which function finds and replaces every instance of a certain pattern in a vector of strings?

A

sub()

Ex: sub(pattern = “a”, replacement = “b”, x = my_vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between the sub and gsub functions?

A

While the sub function only replaces the first match in each string, gsub replaces every match.

17
Q

How can you give multiple options inside grep, grepl, sub, or gsub?

A

Using the | operator.

Ex: gsub(pattern = “a|i”, replacement = “b”, x = my_vector)