R Programming Language Certification Case Studies & Practical Application 4 — Questions and Answers
Question 1: A cybersecurity analyst has a character vector of IP addresses and needs to extract only those matching IPv4 format (e.g., '192.168.1.1'). Which base R function uses a regex for this?
- grep('^(\\d{1,3}\\.){3}\\d{1,3}$', ips, value=TRUE) (Correct answer)
- strsplit(ips, '.', fixed=TRUE)
- substr(ips, 1, 15)
- nchar(ips) == 15
Correct answer: grep('^(\\d{1,3}\\.){3}\\d{1,3}$', ips, value=TRUE)
grep() with value=TRUE returns matching elements; the regex anchors to start and end and matches the four-octet IPv4 pattern.
Question 2: An economist fits a multiple linear regression and suspects heteroskedasticity after plotting residuals vs. fitted values. Which test formally checks for heteroskedasticity in R?
- shapiro.test() on residuals
- bptest() from the lmtest package (Breusch-Pagan test) (Correct answer)
- Box.test() on residuals
- adf.test() from tseries package
Correct answer: bptest() from the lmtest package (Breusch-Pagan test)
The Breusch-Pagan test (bptest() in lmtest) formally tests whether residual variance is constant across fitted values.
Question 3: A data journalist wants to create a choropleth map of US state unemployment rates using ggplot2. After joining unemployment data to a spatial data frame, which geom renders filled polygons for each state?
- geom_polygon(aes(fill=unemployment_rate))
- geom_sf(aes(fill=unemployment_rate)) (Correct answer)
- geom_tile(aes(fill=unemployment_rate))
- geom_map(aes(map_id=state, fill=unemployment_rate))
Correct answer: geom_sf(aes(fill=unemployment_rate))
geom_sf() renders simple features (sf) spatial objects and respects their geometry automatically, making it the modern standard for maps in ggplot2.
Question 4: A clinical trial statistician needs to perform a two-sample t-test assuming unequal variances (Welch's t-test) in R. Which call is correct?
- t.test(group1, group2, var.equal=TRUE)
- t.test(group1, group2, var.equal=FALSE) (Correct answer)
- wilcox.test(group1, group2)
- anova(lm(outcome ~ group))
Correct answer: t.test(group1, group2, var.equal=FALSE)
t.test() defaults to Welch's (var.equal=FALSE), which does not assume equal variances and is appropriate when variance homogeneity is uncertain.
Question 5: A DevOps engineer uses R to automate parsing server log files and needs to read each line of a 500MB log without loading the entire file into memory. Which approach is correct?
- readLines('server.log')
- con <- file('server.log'); open(con); while(length(line <- readLines(con, n=1)) > 0) { ... }; close(con) (Correct answer)
- scan('server.log', what='')
- fread('server.log', header=FALSE)
Correct answer: con <- file('server.log'); open(con); while(length(line <- readLines(con, n=1)) > 0) { ... }; close(con)
Opening a connection and reading n=1 line at a time processes the file sequentially without loading it fully into memory.
Question 6: A marketing data scientist uses caret to train a random forest and wants to tune mtry using 5-fold cross-validation repeated 3 times. Which trainControl setting achieves this?
- trainControl(method='cv', number=5)
- trainControl(method='repeatedcv', number=5, repeats=3) (Correct answer)
- trainControl(method='boot', number=15)
- trainControl(method='LOOCV')
Correct answer: trainControl(method='repeatedcv', number=5, repeats=3)
method='repeatedcv' with number=5 and repeats=3 runs 5-fold cross-validation three times for a total of 15 model fits, providing more stable estimates.
Question 7: A researcher writes an R function that modifies a global variable inside its body using '<<-'. What is the primary risk of this pattern?
- It causes a syntax error because <<- is not a valid operator
- It creates hidden side effects that make the function difficult to test and debug (Correct answer)
- It automatically vectorizes the assignment across all environments
- It prevents the function from returning any value
Correct answer: It creates hidden side effects that make the function difficult to test and debug
Using <<- introduces side effects by modifying variables in parent environments, making code harder to reason about, test, and reproduce.
A cybersecurity analyst has a character vector of IP addresses and needs to extract only those matching IPv4 format (e.g., '192.168.1.1').
Which base R function uses a regex for this?