R1 Flashcards
assign the variable “h” the value 2
h
r workspace
place where variables and information is stored in R.
list all variables in workspace
ls()
remove variable named “a”
rm(a)
clear workspace
rm( list = ls() )
code to multiply 3 and 5
3 * 5
code to calculate 2 to the power of 5
2 ^ 5
code to calculate 28 modulo 6
28 %% 6
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
MY_FRUIT
what is the result of ls() on an empty work space?
character(0)
what is the r variable for pi?
pi
remove variables ‘p’ and ‘q’ from the workspace
rm(p, q)
what are R’s fundamental data types called?
atomic vectors
4 ways to determine object’s type?
typeof() -type of an R object
class() - object oriented programming in R answer
mode()
3 booleans in R
TRUE or T, FALSE or F, NA
class( 2L) returns?
integer
class(2) returns
numeric
is.numeric(2)
TRUE
is.integer(2)
FALSE
class(“string of stuff”)
character
6 Basic Atomic Data Types in R:
logical, integer, double, complex, character, raw
define a vector in R
a vector is an INDEXED SET of values that are all of the same type
in R, data elements are ____, not scalar
vectors
what is the rule for types in vectors?
can only contain one type, can’t mix types
what is the process where a variable’s type is changed?
coercion
coerce logical TRUE to numeric
as.numeric(TRUE)
result of as.numeric(TRUE)
1
result of as.numeric(FALSE)
0
coerce 4 to a character
as.character(4)
can “hello” be coerced into a numeric?
No, as.numeric(“Hello”) returns NA
Can the character string: “4.5” be coerced into a numeric?
Yes. as.numeric(“4.5”) returns 4.5
as.integer(“4.5”) returns 4
which two of the following variables are logical values? TRUE; “hello”; 2L, NA
TRUE, NA
what data type is 4.5
numeric
what data type is 4L?
integer
what is the result of 5 + “five”?
ERROR. non-numeric argument to binary operator
check that 3 is a numeric value and return as boolean
is.numeric(3)
Convert the value in var1 to character and store in variable “var1_car”
var1_car
Convert var2 to a logical: var2_log
var2_log
inspect the class of var2_log
class(var2_log)
coerce var3 to a numeric: var3_num
var3_num
what function is used to create a vector?
c() the c function
name the vector CARDS using the vector SUITS
names(CARDS)
create a vector containing 3 ages and assign each value to a persons name
people
my_apples
is.vector(my_apples)
TRUE
length(my_apples)
1
How are computations on vectors performed?
element-wise
earnings
[1] 20 60 -50
earnings
[1] 50, 200, 90
earnings
[1] 50 50 0
earnings expenses
[1] TRUE TRUE FALSE
# Casino winnings poker
x roulette_vector
stuff
stuff[ 1]
people
people [‘sarah’]
what does ‘recycling’ mean in R?
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
earnings
earnings[‘Monday’]
or
earnings[1]
earnings
earnings[c(2, 4)]
earnings
earnings[2:4]
earnings
earnings[c(‘Monday’, ‘Wednesday’)]
earnings
earnings > 0
earnings
earnings[ c( profitable )]
profit
2
print the number of profitable days
earnings 0
sum( profitable)
print the sum of profitable days
earnings 0
sum( earnings[c( profitable )] )
When using the minus operator for subsetting a named vector, you can subset by:
index
earnings
earnings[-1]
earnings
earnings[-c(1,3)]
e
Monday Wednesday Friday Sunday
5 -1 2 2
What does this do:
assign(“x”, c(10.4, 5.6, 3.1, 6.4, 21.7))
x
Does this cause an error?
c(10.4, 5.6, 3.1, 6.4, 21.7) -> x
No, the arrow assign operator goes both directions
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
y
create a matrix containing values one to six in two rows
matrix(1:6, nrow= 2)
matrix(1:6, nrow= 2)
matrix(1:6, nrow= 2, byrow = TRUE)
What is the difference between these two matrices?
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 does R fill up this 3x2 matrix with 3 values?
matrix(1:3, nrow= 2, ncol = 3)
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
What is the output of:
matrix(1:3, nrow= 2, ncol = 2)
> 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]
What is the output of:
r bind
rbind(1:3, 1:3)
> rbind(1:3, 1:3)
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
What is the output of column bind:
cbind(1:3, 1:3)
> cbind(1:3, 1:3) [,1] [,2] [1,] 1 1 [2,] 2 2 [3,] 3 3
> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
add row with 7, 8, and 9 to m
m m [,1] [,2] [,3] [1,] 1 2 3 [2,] 4 5 6 [3,] 7 8 9
> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
add a column with 10 and 11
m
> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
gives the rows names
rownames(m)
> m
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 4 5 6
Give names to the columns in m
colnames(m) m
col1 col2 col3
[1,] 1 2 3
[2,] 4 5 6
what function can be used to name both rows and columns at the same time?
dimnames
create a matrix for numbers 1 to 6,
with 2 rows
and during creation, name the columns and rows
m m
col1 col2 col3
row1 1 3 5
row2 2 4 6
What happens if a matrix of numbers and a matrix of characters are bound together using rbind or cbind?
coercion
numbers to characters
What data structure(s) in R can contain different types of elements?
dataframe
list
# Star Wars box office in millions (!) box
star_wars_matrix
# Star Wars box office in millions (!) new_hope
> 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
# Star Wars box office in millions (!) new_hope
rownames(star_wars_matrix)
Configure these elements in the correct order to produce pseudocode for using dimnames: = ( , ) ) dimnames list row col
dimnames = list ( row, col ) )
studentID
g
In a matrix of student grades across multiple tests, where each student is a row.
What function will give the students total points?
rowSums()
sums across rows
stems ‘, ‘>’, ‘>’)
leftHeads and one with arrows pointing left
rightArrows
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?
colSums ( )
* note capital letter
What happens when a data sequence that is too short is used to fill up a matrix in R?
R will fill up the matrix column by column and repeat the data sequence.
What are 3 functions that can be used to make a matrix?
rbind ( )
cbind ( )
matrix ( )
What are the TWO advantages of using the function cbind() and rbind() over the function matrix() when creating matrices?
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.
produce a matrix with three rows containing 12 random numeric elements between 1 and 15
m
how do you arrange rows and columns to subset matrix m?
m[ ?? , ??]
m [row, column]
select all elements from matrix m in row three
m[ 3, ]
what data type is returned from:
matrix [ ,3]
what does it contain?
vector with all elements from column 3
what does
matrix [ 4 ] return?
the fourth element in the matrix counting from upper left down each column
> 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
vector of 6 and 7
> 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?
m[1, ‘o1’ ]
> 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
mean (matrix [ ,2] )
> 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”
m[ c(1, 3) , ]
What does this output
matrix[c(FALSE, TRUE, TRUE), c(TRUE, TRUE)]
the last two rows for both columns
What does this return for a 3 row by 2 col matrix?
matrix[ c(FALSE, TRUE, TRUE), ]
The last two rows for both columns
Function to take the sum of each column and store it in a vector
colSums()
function to take the sum of each row and store it in a vector
rowSums()
> m [,1] [,2] a 2 2 b 2 2 > m * 2
What is the output
> m * 2
[,1] [,2]
a 4 4
b 4 4
> m [,1] [,2] a 2 2 b 2 2 > m-1 What is the output
> m-1
[,1] [,2]
a 1 1
b 1 1
> m [,1] [,2] a 2 2 b 3 3 > mm [,1] [,2] [1,] 1 1 [2,] 1 1
what is m + mm
> m + mm
[,1] [,2]
a 3 3
b 4 4
element wise addition
convert
blood
blood_factor
What does factor () function do:
- scans for categories
- store sorts levels alphabetically
- converts the character vector, to a vector of integer values. Integer values map to displayed character values.
what happens if the str function is called on a factor variable?
str ( factor_variable)
shows the number of levels, character displays, and mapped integer values
Rs default order for factor variables is:
alphabetical
blood
blood_factor2
tshirt
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
tshirt
No, must added ordered = TRUE
tshirt_factor
tshirt_factor
TRUE
What is the output of c( “RecordName”, 100, 5)
[1] “RecordName” “190” “5”
R performed coercion to create vector with a single datatype
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
list(Song = “song”, hundred =100, rand =5)
how do you add names to elements in a list?
names ( listName)
Display structure of a list
str ( listName)
What data type is returned:
list [ 1 ]
subsetting a list using single brackets returns a list
what data type is returned:
x
> y [1] "x" > typeof( y) [1] "character" >
x
> x [[ 1]]
[1] “x”
character x
x
> x [[ ‘var1’]]
[1] “x”
character x
x
Error in x[[c(“var1”, “var2”)]] : subscript out of bounds
because double brackets means - return single element from a list
x
> x[ c(‘var1’, ‘var2’)]
$var1
[1] “x”
$var2
[1] 2
> str(x2) List of 2 $ var1: chr "var1" $ var4:List of 1 ..$ var3: chr "var3"
select var3
x2 [[ 2 ]]
> str(x2) List of 2 $ var1: num 1 $ var4:List of 2 ..$ var3: num 3 ..$ var4: num 4 select var3
x2[[ 2 ]] [[ 1 ]]
> str(x2) List of 2 $ var1: num 1 $ var5:List of 2 ..$ var3: num 3 ..$ var4: num 4 subset var5 to a list
x2[[ 2 ]] or x2[ 2 ] or x2$var5
rule of thumb for difference between single and double square brackets for lists?
[ ]
[[ ]]
double brackets [[ to select element
single brackets [ for sublist
> str(shining_list) List of 3 $ title : chr ... $ actors : chr [1:5] ... $ reviews: Ord.factor...
return actors
shining_list$actors
> str(shining_list) List of 3 $ title : chr ... $ actors : chr [1:5] ... $ reviews: Ord.factor...
List containing title and reviews
shining_list[c(‘title’,’reviews’)]
> str(shining_list) List of 3 $ title : chr ... $ actors : chr [1:5] ... $ reviews: Ord.factor... select the last actor
shining_list[[ ‘actors ‘]] [5]
* note use of double and single brackets for chaining selections
x1
> x3