Week 14 · Advanced Statistical Programming using R
Which two commands move into a folder and list its contents?
dir data-raw/ → list()cd data-raw/ → lspwd data-raw/ → openmv data-raw/ → read_csv()Answer: 2) cd changes the working directory, ls lists its contents. pwd prints the current path if you need to check where you are.
Which name is the formal parameter declared by max_minus_min()?
na.rm = TRUEfunction(x)max_minus_minxAnswer: 4) x is the formal parameter declared in function(x). na.rm = TRUE is an argument passed internally to max() and min(), not a parameter of max_minus_min() itself.
The lecture extended the classic DRY rule (“Don’t Repeat Yourself”) to DRRY. What does the extra R stand for?
Answer: 2) DRY targets repetition in the text of your code. DRRY targets cognitive load: the effort to understand your code later on.
What is a silent error?
ggplot2Answer: 3) The code runs and returns an output that looks fine. Silent errors are the hardest to catch, so sense-check every result.
Your function errors deep inside nested calls. Which command shows the sequence of function calls that led to the error?
browser()traceback() or rlang::last_trace()debugonce()stopifnot()Answer: 2) traceback() prints the call stack after an error. browser() pauses execution for interactive inspection, debugonce() triggers browser for one call, and stopifnot() validates inputs.
You edited analysis.R and want to save this change in your Git history. What is the correct order of commands?
git add analysis.R → git commit -m "update"git commit -m "update" → git add analysis.Rgit init → git commit -m "update"git status → git logAnswer: 1) git add stages the change, git commit records it as a snapshot in the repository.
You see this in a file during a merge:
<<<<<<< HEAD
result <- mean(x, na.rm = TRUE)
=======
result <- median(x, na.rm = TRUE)
>>>>>>> feature-branch
What do you do?
sudo mergegit reset --hard to discard both versionsgit add + git commitAnswer: 3) Choose (or combine) the version you want, remove the <<<<<<<, =======, >>>>>>> markers, then mark as resolved.
What does quarto publish gh-pages do?
.qmd source files directly to GitHub Pagesmain branchgh-pages branchAnswer: 4) Quarto renders locally, then pushes the rendered output to gh-pages. Your .qmd sources stay on main.
Which file holds your package’s name, version, licence, and dependencies?
NAMESPACEREADME.mdDESCRIPTION.RbuildignoreAnswer: 3) DESCRIPTION stores package metadata: name, version, licence, and dependencies. NAMESPACE controls exports and imports and is normally generated from roxygen2 comments.
Why should you never write library(ggplot2) inside a package function?
library() calls inside functionslibrary() is slower than require()Answer: 4) Declare ggplot2 under Imports in DESCRIPTION (via usethis::use_package("ggplot2")) and call ggplot2::aes() with namespace qualification.
What does Initial Data Analysis (IDA) refer to?
Answer: 2) IDA is the preparation phase. It checks types, ranges, and missing values so the data matches your assumptions before any inference.
A monthly dataset contains rows for January, February, and April, but no row for March. What kind of missingness is this?
Answer: 3) The expected March row is absent entirely, so the missingness is implicit. Use tidyr::complete() to add the missing row and make the gap explicit as NA.
You clone an renv-managed repo. What is the correct setup sequence?
renv::init() → renv::snapshot()renv::update() → renv::commit()renv::install() → renv::push()renv::restore()Answer: 4) renv::restore() installs every package at the version recorded in renv.lock. Opening the project triggers .Rprofile to activate the local library.
What does FAIR stand for in the context of open data?
Answer: 2) Findable (identifiers), Accessible (open protocols, metadata stays even if data doesn’t), Interoperable (standard formats), Reusable (clear licensing). Introduced by Wilkinson et al. (2016).
What are the three goals of analysis introduced in W10?
Answer: 3) Descriptive asks what is the state of things in the population, Explanatory is there a real relationship, Predictive how well can we predict new cases.
A p-value is the probability of…
Answer: 1) The p-value lives in the world where H₀ is true. It does not tell you the probability of H₀ itself, or the size of any effect.
Should you use a table or a plot to communicate a trend?
Answer: 2) Tables are best for exact values or small comparisons. Plots win for patterns, trends, and messages.
Which tools can restore a reproducible R environment? (multiple correct answers)
renv for R package versionssessionInfo() in a READMEAnswer: 1 & 2)
Correct: 1. renv::restore() rebuilds the R library from renv.lock.
Correct: 2. Docker rebuilds the OS, libraries, and runtimes from a Dockerfile.
False: 3. sessionInfo() documents the environment but cannot restore it.
False: 4. Git tracks code history but does not manage the R environment.
What is the fastest way to make an existing ggplot2 figure explorable in the browser?
ggplotly()gt tableAnswer: 2) ggplotly(p) turns a static ggplot2 object into an interactive plotly chart. Hover, zoom, and legend toggles come along for free.
What does the {reticulate} package do?
Answer: 3) reticulate embeds Python in your R session. R and Python objects are translated on the fly, so a data frame becomes a pandas DataFrame and back.