Atomic Types, Type Coercion, and Logical/Relational Operators
Without running it, what does this code return?
typeof(c(1, 4, 2))
is.logical(FALSE)
as.logical(as.numeric("1234"))
as.logical(as.character(1234))
typeof(c(1L, 4L, 2L))
typeof(c(6L, 4, 2L, 40L))
as.logical(1)
TRUE + FALSE + FALSE^FALSE
FALSE + 1 + (T^999 * 0.5) %/% 3
!TRUE * 4 %/% 2
!0
!1
!-29
typeof(c(1, 1.0, "1.0"))
as.numeric(c(1, 1.0, "1.0", TRUE))
TRUE && !FALSE
5 > 2 * 3 || 9 == 19%/%2
0 && 0
as.numeric(9 != 9 && 10 > 11)
!is.integer(650)
Use sprintf() to a make a Mad Libs style paragraph, incorporating all of the variables in the list below (in no particular order). In other words, you are constructing the shell of the sentence with “blanks” where these words/numbers can be inserted, regardless of their value. All words are lower case (don’t worry about capitalization for now).
exclamation (e.g., “oh no!”)
plural_noun (e.g., “words”)
verb (e.g., “write”)
adverb (e.g., “slowly”)
singular_noun (e.g., “word”)
verb_ing (e.g., “sitting”)
adjective (e.g., “smart”)
food_item (e.g., “rice”)
verbing_in_place (e.g., “eating in Brazil”)
integer (e.g., 1)
double (e.g., 1.2345678)
Write this same exact sentence using paste()
Write this same exact sentence using glue::glue() (you will need to install the glue package if it is not already installed)
The sleep dataset in R (see help(sleep)) are data from within-subjects (ID) experimental research showing the effect of two different drugs (group) on change in sleep (extra). The group variable is currently coded as 1 and 2, which isn’t particularly informative. Change group to be a factor and give it labels (i.e., drug names) of whatever you want! (Note that group is already factor in the data with uninformative labels. For this problem, assume it is a numeric variable).
Coding with Vectors
Use c() to create the following vector
## [1] "Apples" "Oranges" "Bananas" "Ice Cream"
Use : to create an integer vector of the numbers -6 to 20
Use rep() to create a character vector with 14 elements where each even numbered element has the value "even" and each odd numbered element has the value "odd"
Use LETTERS and [] to create a vector of the last 10 capital letters
## [1] "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
Use letters and [] to get every 3rd letter of the lower-case alphabet
## [1] "c" "f" "i" "l" "o" "r" "u" "x"
Create (1) a vector with letters 1 through 9, (2) another vector with LETTERS 10 through 21, and (3) another vector with letters 22 through 26. Then, create a vector called alPHAbet using c() to combine these three vectors together.
Use c() and [] to combine the first 4 element of letters with the 4th, 5th, and 9th, elements of LETTERS into a vector called my_letters
## [1] "a" "b" "c" "d" "D" "E" "I"
Use rev() to reverse the order of letters
## [1] "z" "y" "x" "w" "v" "u" "t" "s" "r" "q" "p" "o" "n" "m" "l" "k" "j" "i" "h"
## [20] "g" "f" "e" "d" "c" "b" "a"
Create a numeric vector called numbers that is 10 elements long with any numbers you would like. Then add 3 to each odd numbered element (i.e. elements 1, 3, 5, etc.) and 7 to each even numbered element.
## [1] 101 102 103 104 105 106 107 108 109 110
## [1] 104 109 106 111 108 113 110 115 112 117
Below is a vector of forcasted temperatures in degrees celcius in Seattle Friday (6/25/21) to Friday (7/2/21). Use the following formula to convert this vector to farenheit and save it in a new vector called tempsF.
∘F=∘C×95+32
tempsC <- c(29, 35, 38, 41, 31, 28, 29, 29)
## [1] 84.2 95.0 100.4 105.8 87.8 82.4 84.2 84.2
Give tempsF names that correspond with the days of the week. The first element (29) is Friday, the second (35) is Saturday, etc.
Using relational operator(s), subset tempsF to days where it was over 100 °F
## Sunday Monday
## 100.4 105.8
Coding with Matrices
Use matrix() to create a matrix called my_matrix with 10 rows and four columns filled with NA
## [,1] [,2] [,3] [,4]
## [1,] NA NA NA NA
## [2,] NA NA NA NA
## [3,] NA NA NA NA
## [4,] NA NA NA NA
## [5,] NA NA NA NA
## [6,] NA NA NA NA
## [7,] NA NA NA NA
## [8,] NA NA NA NA
## [9,] NA NA NA NA
## [10,] NA NA NA NA
Create a vector called vec_num of 10 numbers evenly spaced from 0 to 15. Then assign vec_num to the first column of my_matrix.
## [,1] [,2] [,3] [,4]
## [1,] 0.000000 NA NA NA
## [2,] 1.666667 NA NA NA
## [3,] 3.333333 NA NA NA
## [4,] 5.000000 NA NA NA
## [5,] 6.666667 NA NA NA
## [6,] 8.333333 NA NA NA
## [7,] 10.000000 NA NA NA
## [8,] 11.666667 NA NA NA
## [9,] 13.333333 NA NA NA
## [10,] 15.000000 NA NA NA
Create an integer vector called vec_int of numbers 7 through 16. Then assign vec_int to the second column of my_matrix.
## [,1] [,2] [,3] [,4]
## [1,] 0.000000 7 NA NA
## [2,] 1.666667 8 NA NA
## [3,] 3.333333 9 NA NA
## [4,] 5.000000 10 NA NA
## [5,] 6.666667 11 NA NA
## [6,] 8.333333 12 NA NA
## [7,] 10.000000 13 NA NA
## [8,] 11.666667 14 NA NA
## [9,] 13.333333 15 NA NA
## [10,] 15.000000 16 NA NA
Use letters, LETTERS, and [] to get the first 10 letters to columns 3 and 4 of my_matrix.
Use cov() to get the variance-covariance matrix of the mtcars dataset (all variables are already numeric) and same this to a variable called mtcars_varcov. Then save the covariances of each variable of mtcars (i.e., all elements on the diagonal of mtcars_varcov) to a vector called mtcars_cov.
Coding with Lists
Use list() to create a list that contains vec_num and row_4, and assign the names vec_num and row_4 to these two elements of list_1.
Now copy list_3 as list_4 and use [[]] to assign list_3 as the last (fifth) element of list_4. This is, you should end up with a list with five elements named list_4 that contains the same four elements as list_3 plus a fifth element that is itself a list, as one object.
Select the third element (that is, the sub-element) of the the fifth element of list_4 and assign it to an object called element_5_3 using [[]].
Lastly, repeat the previous assignment of the third element of the fifth element, but extract the element as a list rather than scalar using [] and assign it to list_5_3.
Let’s run a linear regression predicting miles per gallon (mpg) with number of cylinders (cyl), displacement (disp), and weight (wt), saving the result in an object called fit. Using either $ or [[]], extract the coefficients and the residuals. (Hint: to see the names of fit, you can types fit$ in RStudio and it will give you a dropdown menu with all the names. Or you can type names(fit) into your console).
Subset mtcars to get only those cars with the minimum number of cylinders (cyl) and the maximum number of cylinders. Then, using rownames(), print which cars fall in this category. Bonus if you do this programmatically (i.e., do not assume you know the exact min and max values of cyl).