R1 Flashcards

1
Q

assign the variable “h” the value 2

A

h

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

r workspace

A

place where variables and information is stored in R.

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

list all variables in workspace

A

ls()

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

remove variable named “a”

A

rm(a)

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

clear workspace

A

rm( list = ls() )

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

code to multiply 3 and 5

A

3 * 5

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

code to calculate 2 to the power of 5

A

2 ^ 5

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

code to calculate 28 modulo 6

A

28 %% 6

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

WRITE R CODE TO:

Combine the variables MY_APPLES and MY_ORANGES into a new variable MY_FRUIT, which is the total amount of fruits in your fruit basket

A

MY_FRUIT

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

what is the result of ls() on an empty work space?

A

character(0)

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

what is the r variable for pi?

A

pi

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

remove variables ‘p’ and ‘q’ from the workspace

A

rm(p, q)

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

what are R’s fundamental data types called?

A

atomic vectors

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

4 ways to determine object’s type?

A

typeof() -type of an R object
class() - object oriented programming in R answer

mode()

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

3 booleans in R

A

TRUE or T, FALSE or F, NA

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

class( 2L) returns?

A

integer

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

class(2) returns

A

numeric

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

is.numeric(2)

A

TRUE

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

is.integer(2)

A

FALSE

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

class(“string of stuff”)

A

character

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

6 Basic Atomic Data Types in R:

A

logical, integer, double, complex, character, raw

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

define a vector in R

A

a vector is an INDEXED SET of values that are all of the same type

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

in R, data elements are ____, not scalar

A

vectors

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

what is the rule for types in vectors?

A

can only contain one type, can’t mix types

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

what is the process where a variable’s type is changed?

A

coercion

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

coerce logical TRUE to numeric

A

as.numeric(TRUE)

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

result of as.numeric(TRUE)

A

1

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

result of as.numeric(FALSE)

A

0

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

coerce 4 to a character

A

as.character(4)

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

can “hello” be coerced into a numeric?

A

No, as.numeric(“Hello”) returns NA

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

Can the character string: “4.5” be coerced into a numeric?

A

Yes. as.numeric(“4.5”) returns 4.5

as.integer(“4.5”) returns 4

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

which two of the following variables are logical values? TRUE; “hello”; 2L, NA

A

TRUE, NA

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

what data type is 4.5

A

numeric

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

what data type is 4L?

A

integer

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

what is the result of 5 + “five”?

A

ERROR. non-numeric argument to binary operator

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

check that 3 is a numeric value and return as boolean

A

is.numeric(3)

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

Convert the value in var1 to character and store in variable “var1_car”

A

var1_car

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

Convert var2 to a logical: var2_log

A

var2_log

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

inspect the class of var2_log

A

class(var2_log)

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

coerce var3 to a numeric: var3_num

A

var3_num

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

what function is used to create a vector?

A

c() the c function

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

name the vector CARDS using the vector SUITS

A

names(CARDS)

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

create a vector containing 3 ages and assign each value to a persons name

A

people

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

my_apples

A

is.vector(my_apples)
TRUE
length(my_apples)
1

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

How are computations on vectors performed?

A

element-wise

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

earnings

A

[1] 20 60 -50

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

earnings

A

[1] 50, 200, 90

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

earnings

A

[1] 50 50 0

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

earnings expenses

A

[1] TRUE TRUE FALSE

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
50
Q
# Casino winnings
poker
A

x roulette_vector

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

stuff

A

stuff[ 1]

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

people

A

people [‘sarah’]

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

what does ‘recycling’ mean in R?

A

if one vector is applied to a different length vector, R is smart enough to repeats the contents of the shorter vector until it has the same length

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

earnings

A

earnings[‘Monday’]

or
earnings[1]

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

earnings

A

earnings[c(2, 4)]

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

earnings

A

earnings[2:4]

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

earnings

A

earnings[c(‘Monday’, ‘Wednesday’)]

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

earnings

A

earnings > 0

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

earnings

A

earnings[ c( profitable )]

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

profit

A

2

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

print the number of profitable days

earnings 0

A

sum( profitable)

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

print the sum of profitable days

earnings 0

A

sum( earnings[c( profitable )] )

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

When using the minus operator for subsetting a named vector, you can subset by:

A

index

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

earnings

A

earnings[-1]

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

earnings

A

earnings[-c(1,3)]

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

e

A

Monday Wednesday Friday Sunday

5 -1 2 2

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

What does this do:

assign(“x”, c(10.4, 5.6, 3.1, 6.4, 21.7))

A

x

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

Does this cause an error?

c(10.4, 5.6, 3.1, 6.4, 21.7) -> x

A

No, the arrow assign operator goes both directions

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

c(10.4, 5.6, 3.1, 6.4, 21.7) -> x

create a vector y that contains all x elements, a zero, and then all x elements again

A

y

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

create a matrix containing values one to six in two rows

A

matrix(1:6, nrow= 2)

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

matrix(1:6, nrow= 2)
matrix(1:6, nrow= 2, byrow = TRUE)

What is the difference between these two matrices?

A

the first fills values down the column and the second fills across the row, left to right

> matrix(1:6, nrow= 2)
[,1] [,2] [,3]
[1,] 1 3 5

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

How does R fill up this 3x2 matrix with 3 values?

matrix(1:3, nrow= 2, ncol = 3)

A

It recylces, looping through 1-3 twice

> matrix(1:3, nrow= 2, ncol = 3)
[,1] [,2] [,3]
[1,] 1 3 2
[2,] 2 1 3

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

What is the output of:

matrix(1:3, nrow= 2, ncol = 2)

A
> matrix(1:3, nrow= 2, ncol = 2)
     [,1] [,2]
[1,]    1    3
[2,]    2    1
Warning message:
...not a sub-multiple or multiple of the number of rows [2]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
74
Q

What is the output of:
r bind

rbind(1:3, 1:3)

A

> rbind(1:3, 1:3)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3

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

What is the output of column bind:

cbind(1:3, 1:3)

A
> cbind(1:3, 1:3)
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]    3    3
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
76
Q

> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

add row with 7, 8, and 9 to m

A
m  m
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6
[3,]    7    8    9
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
77
Q

> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

add a column with 10 and 11

A

m

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

> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

gives the rows names

A

rownames(m)

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

> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6

Give names to the columns in m

A

colnames(m) m
col1 col2 col3
[1,] 1 2 3
[2,] 4 5 6

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

what function can be used to name both rows and columns at the same time?

A

dimnames

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

create a matrix for numbers 1 to 6,
with 2 rows
and during creation, name the columns and rows

A

m m
col1 col2 col3
row1 1 3 5
row2 2 4 6

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

What happens if a matrix of numbers and a matrix of characters are bound together using rbind or cbind?

A

coercion

numbers to characters

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

What data structure(s) in R can contain different types of elements?

A

dataframe

list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
84
Q
# Star Wars box office in millions (!)
box
A

star_wars_matrix

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
85
Q
# Star Wars box office in millions (!)
new_hope
A
> star_wars_matrix  star_wars_matrix
                  [,1]  [,2]
new_hope       460.998 314.4
empire_strikes 290.475 247.9
return_jedi    309.306 165.8
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
86
Q
# Star Wars box office in millions (!)
new_hope
A

rownames(star_wars_matrix)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
87
Q
Configure these elements in the correct order to produce pseudocode for using dimnames:
= ( , ) )
dimnames
list
row
col
A

dimnames = list ( row, col ) )

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

studentID

A

g

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

In a matrix of student grades across multiple tests, where each student is a row.

What function will give the students total points?

A

rowSums()

sums across rows

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

stems ‘, ‘>’, ‘>’)

leftHeads and one with arrows pointing left

A

rightArrows

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

For a matrix with columns of exams scores, a row for each student.

What function would easily calculate the total points scored for each test?

A

colSums ( )

* note capital letter

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

What happens when a data sequence that is too short is used to fill up a matrix in R?

A

R will fill up the matrix column by column and repeat the data sequence.

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

What are 3 functions that can be used to make a matrix?

A

rbind ( )
cbind ( )
matrix ( )

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

What are the TWO advantages of using the function cbind() and rbind() over the function matrix() when creating matrices?

A

You don’t need to pass it an input vector explicitly that is then converted to a matrix.

You don’t have to explicitly state the way in which the matrix has to be filled.

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

produce a matrix with three rows containing 12 random numeric elements between 1 and 15

A

m

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

how do you arrange rows and columns to subset matrix m?

m[ ?? , ??]

A

m [row, column]

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

select all elements from matrix m in row three

A

m[ 3, ]

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

what data type is returned from:

matrix [ ,3]

what does it contain?

A

vector with all elements from column 3

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

what does

matrix [ 4 ] return?

A

the fourth element in the matrix counting from upper left down each column

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
100
Q
> m
     [,1] [,2] [,3] [,4]
[1,]    1    2    3    4
[2,]    5    6    7    8
[3,]    1    2    3    4
[4,]    5    6    7    8
m[2, c(2,3)] What is returned
A

vector of 6 and 7

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
101
Q
> m
     o1 e1 o2 e2
[1,]  1  2  3  4
[2,]  5  6  7  8
[3,]  1  2  3  4
[4,]  5  6  7  8
Can you subset the upper left element?
A

m[1, ‘o1’ ]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
102
Q
> matrix
               US_revenue non_us
new hope          460.998  314.4
empire strikes    290.475  247.9
returnJedi        309.306  165.8

return the average non_us

A

mean (matrix [ ,2] )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
103
Q
> matrix
               US_revenue non_us
new hope          460.998  314.4
empire strikes    290.475  247.9
returnJedi        309.306  165.8

Subset all data from “A New Hope” and “Return of the Jedi”

A

m[ c(1, 3) , ]

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

What does this output

matrix[c(FALSE, TRUE, TRUE), c(TRUE, TRUE)]

A

the last two rows for both columns

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

What does this return for a 3 row by 2 col matrix?

matrix[ c(FALSE, TRUE, TRUE), ]

A

The last two rows for both columns

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

Function to take the sum of each column and store it in a vector

A

colSums()

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

function to take the sum of each row and store it in a vector

A

rowSums()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
108
Q
> m
  [,1] [,2]
a    2    2
b    2    2
> m * 2

What is the output

A

> m * 2
[,1] [,2]
a 4 4
b 4 4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
109
Q
> m
  [,1] [,2]
a    2    2
b    2    2
> m-1
What is the output
A

> m-1
[,1] [,2]
a 1 1
b 1 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
110
Q
> m
  [,1] [,2]
a    2    2
b    3    3
> mm
     [,1] [,2]
[1,]    1    1
[2,]    1    1

what is m + mm

A

> m + mm
[,1] [,2]
a 3 3
b 4 4

element wise addition

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

convert

blood

A

blood_factor

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

What does factor () function do:

A
  1. scans for categories
  2. store sorts levels alphabetically
  3. converts the character vector, to a vector of integer values. Integer values map to displayed character values.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
113
Q

what happens if the str function is called on a factor variable?

str ( factor_variable)

A

shows the number of levels, character displays, and mapped integer values

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

Rs default order for factor variables is:

A

alphabetical

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

blood

A

blood_factor2

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

tshirt

A

tshirt_factor22 creates a factor variable with 3 levels, it will have the correct 2

tshirt_factor23 specifies the labels for the 3 factors IN THE WRONG ORDER, therefore it will WRONGLY show 2 large shirts

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

tshirt

A

No, must added ordered = TRUE

tshirt_factor

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

tshirt_factor

A

TRUE

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

What is the output of c( “RecordName”, 100, 5)

A

[1] “RecordName” “190” “5”

R performed coercion to create vector with a single datatype

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

Save “song” with the name ‘Song’, 100 with the name ‘hundred’, and 5 with the name ‘rand’

to a single data structure without coercion. name

A

list(Song = “song”, hundred =100, rand =5)

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

how do you add names to elements in a list?

A

names ( listName)

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

Display structure of a list

A

str ( listName)

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

What data type is returned:

list [ 1 ]

A

subsetting a list using single brackets returns a list

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

what data type is returned:

x

A
> y
[1] "x"
> typeof( y)
[1] "character"
>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
125
Q

x

A

> x [[ 1]]
[1] “x”

character x

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

x

A

> x [[ ‘var1’]]
[1] “x”

character x

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

x

A

Error in x[[c(“var1”, “var2”)]] : subscript out of bounds

because double brackets means - return single element from a list

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

x

A

> x[ c(‘var1’, ‘var2’)]
$var1
[1] “x”

$var2
[1] 2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
129
Q
> str(x2)
List of 2
 $ var1: chr "var1"
 $ var4:List of 1
  ..$ var3: chr "var3"

select var3

A

x2 [[ 2 ]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
130
Q
> str(x2)
List of 2
 $ var1: num 1
 $ var4:List of 2
  ..$ var3: num 3
  ..$ var4: num 4
select var3
A

x2[[ 2 ]] [[ 1 ]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
131
Q
> str(x2)
List of 2
 $ var1: num 1
 $ var5:List of 2
  ..$ var3: num 3
  ..$ var4: num 4
subset var5 to a list
A
x2[[ 2 ]] 
or 
x2[ 2 ]
or
x2$var5
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
132
Q

rule of thumb for difference between single and double square brackets for lists?

[ ]
[[ ]]

A

double brackets [[ to select element

single brackets [ for sublist

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
133
Q
> str(shining_list)
List of 3
 $ title  : chr ...
 $ actors : chr [1:5] ...
 $ reviews: Ord.factor...

return actors

A

shining_list$actors

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
134
Q
> str(shining_list)
List of 3
 $ title  : chr ...
 $ actors : chr [1:5] ...
 $ reviews: Ord.factor...

List containing title and reviews

A

shining_list[c(‘title’,’reviews’)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
135
Q
> str(shining_list)
List of 3
 $ title  : chr ...
 $ actors : chr [1:5] ...
 $ reviews: Ord.factor...
select the last actor
A

shining_list[[ ‘actors ‘]] [5]

* note use of double and single brackets for chaining selections

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

x1

A

> x3

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

x2 l
[1] “1” “2” “string”

make l a list rather than a vector to avoid coercion

A

l

138
Q

What function prints the first observations of a dataset?

A

head ()

139
Q

What function prints the last observations of a dataset?

A

tail ()

140
Q

What function prints the dimensions of a dataset?

A

dim()

141
Q

What function shows the structure of a dataset or list?

A

str ()

142
Q

Encode type as a factor: type_factor

type

A

type_factor

143
Q

planets_df

A

planets_df

144
Q

What is a dataframe “under the hood”?

A

a list containing same-length vectors

145
Q

The dataframe planets_df exists and you want to rename its two columns to ‘name’ and ‘distance’

A

names(planets_df)

146
Q

change a column in a dataframe to a factor

A

df$colName

147
Q

change a column in a dataframe to character

A

df$colName

148
Q

create a sequence of 10 letters

A

LETTERS[seq( from = 1, to = 10 )]

149
Q

create a vector of TRUE and FALSE randomly

A

bool1

150
Q

c1

A

identical(c1, c2)

151
Q

c1

A

false

152
Q

Check that c2 is an accurate recode of c1

c1

A
> table(c1, c2)
     c2
c1    2 dog
  2   1   0
  dog 0   1
153
Q

student1

A

be sure that name is set to a factor
all variables must be named
student1

154
Q

> class
name age gpa
1 sarah 26 4
2 jill 21 1

order class dataframe by gpa, lowest first

A

ranks

155
Q

> class
name age gpa
1 sarah 26 1
2 jill 21 4

order the dataset by gpa, highest first

A

ranks

156
Q

What part of a dataframe does this return:

my_df[1,2]

A

first row, second column

157
Q

select rows 1,2, and three and

columns 2,3, and 4 from my_df

A
# rows 1, 2 and 3
# columns 2, 3 and 4
my_df[ 1:3 , 2:4 ]
158
Q

select the first row from my_df

A
# Entire first row
my_df[1, ]
159
Q
> df
  students grades control
1        E     79    TRUE
2        R     96   FALSE
3        Z     75    TRUE

subset control dataframe

A
# boolean vector
controlT
160
Q
> df
  students grades control
1        E     79    TRUE
2        R     96   FALSE
3        Z     75    TRUE

subset grades over 75

A

subset(df, subset = grades>75)
OR
logical 75
df[logical, ]

161
Q

my_df[[“new_column”]]

A

adds column named “new column” to my_df containing values my_vec

162
Q

use cbind to add a column to a dataframe

A

my_df

163
Q

a

A

order(a)
OR
rank(a)

164
Q

a

A

a[order(a)]

165
Q

What function is described:

visualizes the distribution of your data by placing all values in bins and displaying the bin frequencies

A

hist ()

166
Q

create a histogram of variable x with 10 columns

A

hist(x, breaks = 10)

167
Q
df:
 $ rating : num  
 $ votes  : int  
 $ runtime: int  
 $ genre 
# Create a boxplot of the runtime variable
A

boxplot( df$runtime )

168
Q
df:
 $ rating : num  
 $ votes  : int  
 $ runtime: int  
 $ genre 
# Subset rating, votes and runtime and plot all 3
A

toPlot

169
Q

Create a pie chart from movies$genre

A

pietable

170
Q

view 10

the result is:

A

> view > 10

[1] TRUE FALSE FALSE

171
Q

views checks

A

> views > checks

[1] TRUE TRUE TRUE

172
Q

How does this expression evaluate?

“Rchitect” == “rchitect”

A

FALSE

R is case sensitive

173
Q

How does this expression evaluate?

TRUE == 1

A

TRUE

becauseTRUE coerces to 1 under the hood

174
Q

How does this expression evaluate?

“dog”

A

FALSE

R determines the greater than relationship based on alphabetical order.

175
Q

How does this expression evaluate?

“raining”

A

TRUE

176
Q

linkedin

A

> linkedin > 15

[1] TRUE FALSE FALSE FALSE FALSE TRUE FALSE

177
Q

linkedin

A

views

178
Q

what happens with the double &&

> c(TRUE, TRUE, FALSE) && c(TRUE, FALSE, FALSE)

A

[1] TRUE

only evalutes the first element

179
Q

linkedin

A

14

180
Q

linkedin

A

x

181
Q

!!FALSE

evaluates to:

A

FALSE

182
Q

Count the number of TRUES:
> extremes
[1] TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE

A

> sum(extremes)

[1] 3

183
Q

if condition) {
expr1
}

insert an else that evaluates “expr2”

A
if condition) {
  expr1
} else {
  expr2
}
*else keyword comes on the same line as the closing bracket of the if part!
184
Q

speed

A

print ( paste( “Your speed is “, speed)

185
Q
speed  30) {
  # stop  the while loop when speed    exceeds 80
  }
A
speed  30) {
    if (speed > 80){
     break  ## break
     } 
}
  }
186
Q
# The linkedin vector has already been defined for you
linkedin
A
# Loop version 2
for (i in 1:length(linkedin)){
    print  ( paste(linkedin[i], i))
    }
187
Q
# The linkedin vector has already been defined for you
linkedin
A
# Loop version 1
for (l in linkedin){
    print(l)
    }
188
Q

primes_vec

A

primes_vec[4] # equals 7

primes_list[[4]]

189
Q

for (i in 1: length(nyc)){
print (is.list(nyc [i] )) }

for (i in 1: length(nyc)){
print (is.list(nyc [[i]] )) }
if applied to a list, what do these print out?

A

single brackets prints true for each element, it returns a list

[double brackets prints false for each elements, it returns a vector

190
Q

write a nested for loop to print out:

“On row i and column j, matrix contains x”

A
# define the double for loop
for (i in 1 :nrow (matrix))  {
    for (j in 1 :ncol (matrix))  {
print( paste( 
        "On row ", i, " and column ", j,
        " matrix contains ", matrix [i,j]  ) )
        }
}
191
Q

what word exits a loop

A

break

192
Q

what word skips the remainder of the code in the loop, but continues the iteration.

A

next

193
Q

If a vector value is

A

for (e in matrix) {

if (e

194
Q

what pseudocode would count all the uses of R or r up until the letter ‘u’?
quote

A

set rcount

195
Q

ask if ‘u’ and ‘u’ are the same

ask if ‘u’ and ‘u’ are not the same

A

‘u’ == ‘u’

‘u’ != ‘u’
!’u’ == ‘u’

196
Q

Print “this is” and the value of x

A

print ( paste ( “this is “, x) )

197
Q
the documentation for sd ()
is "sd (x, na.rm = FALSE)"
what happens if we give it a vector writing
sd (x = vector, na.rm = FALSE) OR
sd (vectors)
A

sd (x = vector, na.rm = FALSE)
explicitly assigns each vector value to x as r evaluates element wise

sd(vectors)
the function knows the first parameter is x, then it evaluates element wise

198
Q

values

A

NA

because na.rm is FALSE so sd did not remove the missing values.

rather sd( values, na.rm = TRUE)

199
Q

values

A

sd (values, TRUE)
or
sd (values, na.rm = TRUE)

WRONG - sd(values), gives NA

200
Q

get arguments for a function

A

args ( )

201
Q

get help documentation for a function

A

? function
?? function
help (function)

202
Q

What is the … in r method definitions?

e.g. mean (x, …)

what is it’s purpose?

A

the ellipsis

a way for R to pass arguments to or from other methods without the function having to name them explicitly.

203
Q

mean(x, trim = 0, na.rm = FALSE, …)

in the above definitions, time and na.rm are __________ aguments becaue they have default values

A

optional

204
Q

Calculate the mean of the element-wise sum of the vector linkedin

A

mean ( linkedin )

  • note mean ( sum (linkedin) )
    would take the mean of one value
205
Q

Calculate the mean of the element-wise sum of linkedin and facebook

A

mean ( (linkedin + facebook) )

206
Q

remove missing values from a vector

A

vector

207
Q

triple

A

Yes, r automatically returns the last value

208
Q

triple

A

the character “dog”, the last value referenced

209
Q

triple

A

y, the last value referenced

210
Q

triple

A

triple

211
Q

Is the return() in a function similar to:

  1. break in a for loop
  2. next in a for loop
A
  1. break in a for loop

the function stops evaluating, and ignores the rest of the fxn, and returns

212
Q

Create a function pow_two(): it takes one argument and returns that number squared (that number times itself).

A

pow_two

213
Q

create a function sum_abs(), that takes two arguments and returns the sum of the absolute values of both arguments.

A
# Create a function sum_abs()
sum_abs
214
Q

what can be done to make a function return nothing?

A

return ( NULL )

215
Q

Develop a new function, my_filter(), that takes a single argument and that simply returns the function’s input if it’s positive. If it’s negative, have my_filter() return NULL.

A

my_filter

216
Q

print(paste(“h”, “i”)

prints:
[1] “hi”
or
[1] “h i”

A

> print(paste(“h”, “i”))
[1] “h i”

change sep to nothing
> print(paste(“h”, “i”, sep = “”))
[1] “hi”

217
Q

triple

A

5,

the value of a was not changed,
a

218
Q

what is the implication of r passing variables “by value” to functions?

A

If R were to pass variables “by reference”, changes in the function would change value of variable.
However, R passes “by value”, so the R objects you pass to a function can never change unless you do an explicit assigment.

219
Q

sample

A

> sample(v)

[1] 1 2 3

220
Q

what returns?

sample 2){
return (x)}}
sample(c( 1,3,3))
sample(c( 3,1,1))

A
> sample(c( 1,3,3))
Warning...
> sample(c( 3,1,1))
[1] 3 1 1
Warning ..
#Only the first element is checked by if
221
Q

linkedin

A

sumOver10 10){

sum

222
Q

load package ggivs

A

library( “ggvis” )

223
Q

list packages loaded

A

search()

224
Q

What are two way to load packages?

How does each respond when called to load package that is not installed?

A

library ()
error message

require()
gives warning message
returns FALSE

225
Q

how do I get a package not on my computer?

how do I import a package to the session?

A

install.packages ( )

library()
or require()
loaded packages are attached so search list and available in current session
226
Q

library(ggvis)
library(“ggvis”)

Are these two correct?

A

yes

227
Q

foo

A

> library(foo,character.only=TRUE)
library(foo,character)
Error in library(foo, character) : there is no package called ‘foo’

228
Q

words

A

v

229
Q

words

A

words

230
Q

correctly arrange:

lapply
data
function
… ( additional args e.g. parameters)

A

lapply (data, function, …)

231
Q

how can you cange a list to a vector?

A

unlist ()

232
Q

pioneers

A

strsplit(pioneers,”:” )

233
Q
> str(split_math)
List of 4
 $ : chr [1:2] "GAUSS" "1777"
 $ : chr [1:2] "BAYES" "1702"
# Convert to lowercase strings
A

split_low

234
Q
> str(split_low)
List of 4
 $ : chr [1:2] "gauss" "1777"
 $ : chr [1:2] "bayes" "1702"
 create a function and use lapply to create a list of only the names
A

select_first

235
Q

Functions in R are o_ _ _ _ _ _ _

This means that they aren’t automatically bound to a name.

A

objects

236
Q
# Named function
triple
A
# Anonymous function with same implementation
function(x) { 3*x }
237
Q
# Anonymous function
function(x) { 3*x }

use lapply to perform above function on list(1,2,3)

A

lapply(list(1,2,3), function(x) { 3*x })

238
Q

select_el

A

lapply(S, select_el, index = 1)

239
Q

lapply() always returns a:

A

list

240
Q

A pre-defined function returns all NULLs when used in lapply but works fine when called WITHOUT assignment.
What could be the cause?

A

May use invisible() behind the scenes, which returns an invisible copy of the return value, NULL in this case.

241
Q

sapply is short for

A

simplify apply

242
Q

under the hood, sapply () calls ___________() and then uses ________ to ______ to conver the list output to an array.

A

lapply()

simplify2array()

243
Q

sapply’s USE.NAMES parameter is set to:
TRUE
FALSE

A

TRUE

244
Q

Calculates the average of the min and max of a vector: extremes_avg

can use *apply

A

extremes

245
Q

Create a function thattakes a vector and returns all values below zero

A

below_zero

246
Q

sapply and lapply will give the same output when…

A

it returns vectors of different lengths

247
Q

Will sapply() simplify a list of NULL’s?

A

No,
because the ‘vector-version’ of a list of NULL’s would simply be a NULL, which is no longer a vector with the same length as the input.

248
Q
sapply(list(runif (10), runif (10)), 
       function(x) c(min = min(x), mean = mean(x), max = max(x)))

This code generates a matrix with __ rows and ___ columns.

A

3 rows and 2 columns.

249
Q

How does the length of the input list for lapply relate to the length of the output list?

A

same length

250
Q

What is the danger of sapply?

A

simplifies output to array or returns the same list as lapply

dangerous because output type depends on specifics of input

251
Q

what makes vapply different than sapply or lapply?

A

must specify output data format

252
Q

List of 2
$ : num [1:5] 3 7 9 6 -1
$ : num [1:5] 6 9 12 13 5
basics

A

vapply ( temp, basics, numeric(3))

253
Q

basics

A

basics

254
Q

What’s an easy way to fix the FUN.VALUE arg input in vapply if it is wrong?

A

Read the error message:
>
Error: values must be length 3,
but FUN(X[[1]]) result is length 4

255
Q

Why is vapply() considered a more robust version of sapply(),

A

because you explicitly restrict the output of the function you want to apply

256
Q

strsplit(string, “”)

returns what TYPE of output?

A

list

unlist (output)

257
Q

Sort the vectors inside a list alphabetically

hint: use *apply and a function

A

abcSort

258
Q

round these numbers. what is the output:

n

A

round(n)

1 7 5 3

259
Q

create a vector that has the even numbers 2 through 8

A

seq(2,8, by = 2)

260
Q

print(“hello”)

use an builtin r functio to print hello twice

A

rep (print(“hello”), times = 2)

261
Q

c

A

sort(c)

262
Q

v

A

> rep(v, times= 2)
[1] 2 1 4 2 1 4
rep(v, each = 2)
[1] 2 2 1 1 4 4

263
Q

v

A

append(v, 4)

264
Q

v

A

append(v, 4)

265
Q

s

A

v

266
Q

x

A

sum( abs ( round (x) ) )

267
Q

What function:

Generate sequences, by specifying the from, to and by arguments.

A

seq()

268
Q

Replicate elements of vectors and lists.

A

rep()

269
Q

Arrange a vector in ascending order. Works on numerics, but also on character strings and logicals.

A

sort()

270
Q

Reverse the elements in a data structures for which reversal is defined.

A

rev()

271
Q

Display the structure of any R object.

A

str()

272
Q

Merge vectors or lists.

A

append()

273
Q

Convert an R object from one class to another.

A

as.*()

274
Q

Flatten (possibly embedded) lists to produce a vector.

A

unlist()

275
Q

fix this sort function so it returns the highest results first:

sort (vec )

A

sort(vec, decreasing = TRUE)

276
Q

List of 3
$ : num 1.1
$ : num 3
$ : num 5

sum this list named “x”

A

sum( unlist( x) )

277
Q

Create a sequence that ranges from 1 to 500 in increments of 3. Assign the resulting vector to a variable seq1.

A

seq1

278
Q

find indicies that are true from a logical vector

A

which ( )

279
Q

what is the difference between

sub() and gsub ()

A

The g stands for global, as in replace globally (all)

sub will only replace the first instance of a match in a string

280
Q

list

A

list [index ]

281
Q

what arguments are necessary to search a string vector for ‘s’ with grep or grepl

A

grep ( x = vectorName, pattern = ‘s’)

282
Q

ADD MORE regex

A

xx

283
Q

assign today’s date to the variable “today”

A

today

284
Q

What is the syntax difference below:

Sys.Date()
Sys.time()

A

Date has a capital and time is lower case

285
Q

make a date object out of:

“2016-01-21”

A

as.Date(“2016-01-21” )

286
Q

What does this do:

my_date

A

the Date object is default to year - month - date so will throw an error.

This explicitly assigns each so the alternate order can be used.

287
Q

What is an easy method to find the difference between two dates objects?

A

subtract them!

288
Q

what function changes data.frame into a data table?

what package must be loaded?

A

tbl_df()

dplyr

289
Q

What is the class of a data table?

A

it is a data frame class so it has all the functionality of a data frame AND more

290
Q

What function in dplyr is s a little like str applied to a data frame
but it tries to show you as much data as possible

A

glimpse ( )

291
Q

print the possible values of a factor variable, all unique values present in that variable

A

unique( v )
OR
just print our the variable and it will show the levels

292
Q

Instead of data types R has…

A

data objects

293
Q

How do you get the name of the current working directory in R?

A

getwd()

294
Q

How R is used in logistic regression?

A

Logistic regression deals with measuring the probability of a binary response variable. In R the function glm() is used to create the logistic regression.

295
Q

How do you access the element in the 2nd column and 4th row of a matrix named M?

A

M[4,2]

296
Q

What is recycling of elements in a vector?

A

When two vectors of different length are involved in a operation then the elements of the shorter vector are reused to complete the operation.

297
Q

Can we update and delete any of the elements in a list?

A

We can update any of the element but we can delete only the element at the end of the list.

298
Q

Give the general expression to create a matrix in R.

A

The general expression to create a matrix in R is - matrix(data, nrow, ncol, byrow, dimnames)

299
Q

What is the output of runif(4)?

A

It generates 4 random numbers between 0 and 1.

300
Q

What is expected from running the command - strsplit(x,”e”)?

A

It splits the strings in vector x into substrings at the position of letter e.

301
Q

Give a R script to extract all the unique words in uppercase from the string - “The quick brown fox jumps over the lazy dog”.

A

x

302
Q

> str(x)
List of 1
$ : int [1:4] 5 6 7 8

return the first element of x

A
> str(x[1])
List of 1 $ : int [1:4] 5 6 7 8
> str(x[[1]])
 int [1:4] 5 6 7 8
> str(x[[1]][1])
 int 5
303
Q

X is the vector c(5,9.2,3,8.51,NA), What is the output of mean(x)?

A

NA

304
Q

How do you convert the data in a JSON file to a data frame?

A

Using the function as.data.frame()

305
Q

Give a function in R that replaces all missing values of a vector x with the sum of elements of that vector?

A

function(x) { x[is.na(x)]

306
Q

Is an array a matrix or a matrix an array?

A

Every matrix can be called an array but not the reverse. Matrix is always two dimensional but array can be of any dimension.

307
Q

How to find the help page on missing values?

A

?NA

308
Q

How do you get the standard deviation for a vector x?

A

sd(x, na.rm=TRUE)

309
Q

How do you set the path for current working directory in R?

A

setwd(“Path”)

310
Q

What does col.max(x) do?

A

Find the column has the maximum value for each row.

311
Q

How do you remove a vector from the R workspace?

A

rm(x)

312
Q

List the data sets available in package “MASS”

A

data(package = “MASS”)

313
Q

What is the use of the command - install.packages(file.choose(), repos=NULL)?

A

It is used to install a r package from local directory by browsing and selecting the file.

314
Q

Give the command to check if the element 15 is present in vector x.

A

15 %in% x

315
Q

What is the difference between subset() function and sample() function in R?

A

The subset() functions is used to select variables and observations. The sample() function is used to choose a random sample of size n from a dataset.

316
Q

check if all values in a matrix are not equal to NA

A

missing

317
Q

What is the use of “next” statement in R?

A

The “next” statement in R programming language is useful when we want to skip the current iteration of a loop without terminating it.

318
Q

> sample 2){
+ return(x) } }
v

A
MUST VECTORIZE IF
sample  0, return ("neg"), return(1))
}
OR 
lapply(v, sample)
319
Q

tV

A

it is unchanged so 1,2 because the newly appended vector was not saved to the tV variable

320
Q

tV

A

append(tV, 3)

tV[3]

321
Q

To make R treat these values as nominal variables instead of numbers, you should use what function? T

A

v2

322
Q

What function can be used on a vector to change numeric values or ordinal values?

A

factor( v , order = TRUE, levels = c(“Low”,”Medium”,”High”))

323
Q

Quasi-experimental design means

A

independent variables that cannot be randomly assigned - e.g. sex, medical condition

324
Q

call the method describe from the psych package on data.frame df

A

psych:::describe(df)

325
Q

sapply(mydata, mean)

what should be added to the above call for a real data set?

A

na.rm=TRUE

326
Q

phrase to remember skew direction:

“the skew is where there’s ____”

A

“the skew is where there’s few”

327
Q

Ways distributions can be not normal:

A

bi-modal (two peaks - two groups sampled)

pos. skew
neg. skew
platykurtic (flat)
leptokurtic (spike at mean)

328
Q

function to create z-scores

A

scale()

scale(x, center = TRUE, scale = TRUE)

329
Q

does what?

par(mfrow = c(1,2))

A

plot two side by side

330
Q

the peak or highest point of a histogram is the value for what measurement of central tendency?

A

Mode

331
Q

a distribution with extreme scores is best described using what measure of central tendency?

A

Median - less biased by extreme scores than mean

332
Q

what measure of central tendency can be used to describe nominal data?

A

mode - most frequently ocuring

333
Q

command to find packages attached, data sets, and much much more..

A

sessionInfo()

334
Q

create a vector x that contains a regular sequence of length 100 between -4 and 4.

A

x

335
Q

Create a subset for the dataframe x for when column “level” is 1 (experimental )

A

new

336
Q

add a column that is twice the value of another column in the dataframe

A

transform(df, newCol = oldCol * 2)

337
Q

for(i in 1:nrow(df)){
if (df$column[i] == x) {
df$column2[i]

A

df$column2[df$column1 == x]

338
Q

how to import a a single function from a library

A

import::from(libraryName, functionName)

339
Q

Open data so that it can be inspected in another tab

A

View(data)

  • capital V
340
Q

see the names for all variables

A

labels (data )

341
Q

From a large dataset, grab only the variables/columns ‘a’ and ‘b’

A

cols