R Programming Language Certification Case Studies & Practical Application 2 — Questions and Answers
Question 1: A data scientist is analyzing hospital readmission rates and needs to join patient demographics with clinical outcomes across two large data frames. Which tidyverse approach is most efficient?
- Use merge() from base R with all=TRUE
- Use dplyr's left_join() specifying the key column (Correct answer)
- Use rbind() to stack the data frames
- Use apply() to iterate over rows and match manually
Correct answer: Use dplyr's left_join() specifying the key column
dplyr's left_join() is idiomatic, readable, and efficient for combining data frames on a shared key column.
Question 2: A retail analyst wants to identify seasonal sales patterns in 5 years of daily transaction data. Which R function creates a time series object from a numeric vector with monthly frequency?
- ts(data, frequency=12, start=c(2019,1)) (Correct answer)
- xts(data, order.by=dates)
- zoo(data, order.by=dates)
- timeSeries(data, charvec=dates)
Correct answer: ts(data, frequency=12, start=c(2019,1))
ts() is the base R function for creating time series objects, with frequency=12 for monthly data and start specifying the beginning period.
Question 3: An environmental researcher has a dataset with 30% missing PM2.5 readings. She wants to impute missing values using the median of non-missing values in the same monitoring station group. Which approach achieves this?
- df %>% mutate(pm25 = ifelse(is.na(pm25), mean(pm25, na.rm=TRUE), pm25))
- df %>% group_by(station) %>% mutate(pm25 = ifelse(is.na(pm25), median(pm25, na.rm=TRUE), pm25)) (Correct answer)
- df %>% fill(pm25, .direction='down')
- df %>% mutate(pm25 = na.omit(pm25))
Correct answer: df %>% group_by(station) %>% mutate(pm25 = ifelse(is.na(pm25), median(pm25, na.rm=TRUE), pm25))
group_by(station) ensures the median is computed per station, and mutate with ifelse replaces NAs while preserving observed values.
Question 4: A financial analyst runs a logistic regression to predict loan defaults and gets a warning: 'fitted probabilities numerically 0 or 1 occurred.' What does this indicate?
- The model has too few predictors and underfits the data
- Complete separation exists in the data, causing convergence issues (Correct answer)
- The response variable is continuous rather than binary
- The training set is too large for glm() to handle
Correct answer: Complete separation exists in the data, causing convergence issues
Complete or quasi-complete separation means a predictor perfectly predicts the outcome in a subset, causing extreme coefficient estimates and convergence warnings.
Question 5: A marketing team needs a reproducible report that combines R code, narrative, and output charts for a quarterly campaign analysis. What is the most appropriate tool?
- Write a plain .R script with inline comments
- Use R Markdown (.Rmd) to weave prose, code, and output into a single document (Correct answer)
- Export results to Excel and add comments manually
- Use Shiny to build an interactive dashboard instead
Correct answer: Use R Markdown (.Rmd) to weave prose, code, and output into a single document
R Markdown integrates code, narrative, and rendered output in one reproducible document that can be rendered to HTML, PDF, or Word.
Question 6: A statistician is bootstrapping a 95% confidence interval for the median household income using 10,000 resamples. Which base R function draws samples with replacement?
- replicate(n, median(data))
- sample(data, size=length(data), replace=TRUE) (Correct answer)
- resample(data, n=10000)
- rnorm(length(data), mean=mean(data))
Correct answer: sample(data, size=length(data), replace=TRUE)
sample() with replace=TRUE draws a bootstrap resample of the same size as the original dataset with replacement.
Question 7: A public health researcher uses ggplot2 to visualize COVID-19 case counts on a log scale because the data spans several orders of magnitude. Which layer achieves log10 transformation of the y-axis?
- + scale_y_log10() (Correct answer)
- + coord_trans(y='log10')
- + stat_log(axis='y')
- + theme(axis.y = element_log())
Correct answer: + scale_y_log10()
scale_y_log10() applies a log10 transformation to the y-axis scale, including axis ticks and gridlines, within a ggplot2 plot.
A data scientist is analyzing hospital readmission rates and needs to join patient demographics with clinical outcomes across two large data frames.
Which tidyverse approach is most efficient?