Upload a PDF file, named with your UC Davis email ID and homework number (e.g., xtai_hw1.pdf
), to Gradescope (accessible through Canvas). You will give the commands to answer each question in its own code block, which will also produce output that will be automatically embedded in the output file. Each answer must be supported by written statements as well as any code used.
All code used to produce your results must be shown in your PDF file (e.g., do not use echo = FALSE
or include = FALSE
as options anywhere). Rmd
/qmd
files do not need to be submitted, but may be requested by the TA and must be available when the assignment is submitted.
Students may choose to collaborate with each other on the homework, but must clearly indicate with whom they collaborated.
Execute each of these instructions consecutively (e.g., if a vector was modified in a previous step, use the modified version in the next step).
Use sample()
, sampling with replacement, to create a vector of length 20. (Use the help command ?sample
to learn about the function. You can put anything you’d like in the vector, i.e., any suitable input to the first argument of sample()
.) Print it and check if there are missing values. Find the sum and mean of the vector. Assign the 5th value of the vector the value 0. Create a new vector which only has elements of the first vector that are larger than 5.
The colon operator will create a sequence of integers in order. It is a special case of the function seq()
. Using the help command ?seq
to learn about the function, design an expression that will give you the sequence of numbers from 1 to 10000 in increments of 372. Design another that will give you a sequence between 1 and 10000 that is exactly 50 numbers in length. The function rep()
repeats a vector some number of times. Explain the difference between rep(1:3, times = 3)
and rep(1:3, each = 3)
.
Create a \(3 \times 3\) matrix containing the elements 1 to 9 (filled row-wise). Print the second row, first column of the matrix. Multiply the matrix by 3, element-wise. For each row, sum the columns of the matrix. Use apply()
to do the same. Use two separate apply()
operations, the sum and the number of elements, to find the column means of the matrix.
Create an empty list of length 2. Assign the vector c(2, 4, 6, 1)
to the first element of the list. Assign the vector c(1, 5, 3, 2)
to the second element of the list. Add a third element to the list, with the sequence 1:4. Check what is in the list. Name the list items a, b and c. Find the sum of each of the vectors in the list, using lapply()
. Create a new vector of length 3 with this information using unlist()
.
For the following series of commands, either explain their results, or why they should produce errors.
letters[1] + 1
letters[1] + letters[2]
x <- "7"
x + 2
How can we make the last command (x + 2
) work?
vector1 <- c("5", "12", "7", "32")
max(vector1)
sort(vector1)
sum(vector1)
vector2 <- c("5", 7, 12)
vector2[2] + vector2[3]
list4 <- list(z1 = "6", z2 = 42, z3 = "49", z4 = 126)
list4[[2]] + list4[[4]]
list4[2] + list4[4]
sessionInfo()
## R version 4.4.0 (2024-04-24)
## Platform: x86_64-apple-darwin20
## Running under: macOS Sonoma 14.6.1
##
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/4.4-x86_64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
##
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
##
## time zone: America/Los_Angeles
## tzcode source: internal
##
## attached base packages:
## [1] stats graphics grDevices utils datasets methods base
##
## loaded via a namespace (and not attached):
## [1] digest_0.6.36 R6_2.5.1 fastmap_1.2.0 xfun_0.46
## [5] cachem_1.1.0 knitr_1.48 htmltools_0.5.8.1 rmarkdown_2.27
## [9] lifecycle_1.0.4 cli_3.6.2 sass_0.4.9 jquerylib_0.1.4
## [13] compiler_4.4.0 rstudioapi_0.16.0 tools_4.4.0 evaluate_0.24.0
## [17] bslib_0.8.0 yaml_2.3.10 rlang_1.1.4 jsonlite_1.8.8