Sequences, Vectors, Matrices, and Data Frames Flashcards

1
Q

Create a sequence of numbers from a to b

A

a:b or seq(a, b)

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

Create a sequence of numbers from 1 to 10, incremented by 0.5

A

seq(1, 10, by=0.5)

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

Get the length of a sequence

A

length(sequence)

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

Make a vector with 10 repetitions of the vector (0, 1, 2)

A

rep(c(0, 1, 2), times = 10)

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

Make a vector that repeats 10 zeros, then 10 ones, then 10 twos

A

rep(c(0, 1, 2), each=10)

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

What are the 2 types of vectors?

A

atomic vectors = contain one data type lists = can contain multiple data types

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

Conditional expressions: symbol for OR symbol for AND

A

or: A | B
and: A & B

Note that AND operators get evaluated before OR operators

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

Join together the elements in the following vector: c(“My”, “name”, “is”)

A

paste(c(“My”, “name”, “is”), collapse= “ “) #collapse tells R that want spaces between characters when join together

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

Add an element to this vector: my_char = c(“My”, “name”, “is”)

A

c(my_char, “Estefy”)

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

Join “Hello” and “world” with a space between words

A

paste(“Hello”, “world”, sep = “ “)

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

Generate the following output: A-1, B-2, C-3 Use vectors 1:3 and c(“A”, “B”, “C”)

A

paste(c(“A”, “B”, “C”), 1:3)

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

What do mathematical operations involving NA yield?

A

NA

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

Which function tells you whether each element of a vector is NA?

A

is.na(dataset) #It generates a vector of booleans with TRUE for missing values

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

Count the number of missing values in my_na = is.na(dataset)

A

sum(my_na) This will add up the boolean array where TRUE = 1 and FALSE = 0, giving number of missing values

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

Make a vector of all non-missing values in vector x

A

x[!is.na(x)]

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

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

A

x[c(3, 5)]

x[c(-3, -5)] or x[-c(3, 5)]

17
Q

Create a numeric vector with 3 named elements:

foo=11

bar =2

norf= NA

Print the names of the vector

A

vect <- c(foo = 11, bar = 2, norf = NA)

names(vect)

Output: “foo” “bar” “norf”

18
Q

Add names to vector = c(1, 2, 3)

names: “hey”, “there”, “you”

A

names(vector) <- c(“hey”, “there”, “you”)

19
Q

How do you check if two vectors are the same?

A

identical(vector1, vector2)

Output will be true or false

20
Q

True or False

Matrices can contain many different classes of data

data frames can contain many different classes of data

A

FALSE: matrices can only contain a single class of data

TRUE: data frames can contain many different classes of data

21
Q

add dimension 4, 5 to a vector x

A

dim(x) <- c(4, 5)

22
Q

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”

A

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

23
Q

TRUE function

exclusive OR function

A

xor() returns true if one argument is TRUE and the other one is FALSE, otherwise, it will return FALSE

isTrue(expression)

xor(expression1, expression2)

24
Q

Add the rows of my_matrix

Add the columns of my_matrix

A

rowSums(my_matrix)

colSums(my_matrix)

25
Q

add matrix2 to matrix 1 as columns

add matrix2 as rows to matrix1

A

cbind(matrix1, matrix2)

rbind(matrix1, matrix2)