Week 2 · Advanced Statistical Programming using R
You edited a file called analysis.R and now want to save this change in your Git history. What is the correct order of commands?
git commit -m "update" → git add analysis.Rgit add analysis.R → git commit -m "update"git init → git commit -m "update"git status → git logAnswer: 2) git add stages the change, and git commit records it as a snapshot in the repository.
Which dplyr function would you use to keep only rows where species == "Adelie"?
select(species == "Adelie")filter(species == "Adelie")mutate(species == "Adelie")summarise(species == "Adelie")Answer: 2) filter() keeps rows that satisfy a condition.
What is the main purpose of writing a function?
Answer: 3) functions package code that can be reused, tested and given a meaningful name.
Which part of this function definition is the function argument?
Answer: 4) x is the input supplied to the functions
What does R return if a function has no explicit return() statement?
Answer: 4) R automatically returns the value of the last evaluated expression. You can also use return() - useful for early exits or to make intent clearer.
What is a “silent error”?
ggplot2Answer: 3) The code runs and returns an output. Silent errors may be overlooked.
What is the main problem of writing a function that does too many things at once?
Answer: 2) Hard to test in isolation, debug and reuse. Good functions help with readability and maintainability.
We wrote a trader function that tells us what to do with our stocks.
Which output would the function return?
Answer: 2) The price is neither below buy_below nor above sell_above, so the function returns "hold".
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 - effort to understand your code later on.
What will this function output?
vecvec and the length of vecvecAnswer: 4) f is a recursive function adding the last item of vec to the sum of the rest.