R Programming Language Certification Case Studies & Practical Application 5 — Questions and Answers
Question 1: An urban planner uses R to analyze bike-share trip data and needs to calculate trip duration in minutes from two POSIXct columns (start_time and end_time). Which expression is correct?
- end_time - start_time
- as.numeric(difftime(end_time, start_time, units='mins')) (Correct answer)
- end_time - start_time / 60
- strptime(end_time) - strptime(start_time)
Correct answer: as.numeric(difftime(end_time, start_time, units='mins'))
difftime() with units='mins' computes the difference and as.numeric() converts the difftime object to a plain numeric value in minutes.
Question 2: A NLP researcher tokenizes customer reviews and builds a document-term matrix. After TF-IDF weighting, she wants to reduce dimensions before clustering. Which R function performs PCA?
- factanal(dtm_tfidf, factors=10)
- prcomp(dtm_tfidf, scale.=TRUE) (Correct answer)
- cmdscale(dist(dtm_tfidf))
- umap(dtm_tfidf)
Correct answer: prcomp(dtm_tfidf, scale.=TRUE)
prcomp() performs PCA; scale.=TRUE standardizes variables before decomposition, which is important when features are on different scales.
Question 3: A supply chain analyst has daily demand data and fits an ARIMA model using the forecast package. After fitting, which function generates a 30-day ahead point forecast with prediction intervals?
- predict(fit, n.ahead=30)
- forecast(fit, h=30) (Correct answer)
- arima.forecast(fit, steps=30)
- tsforecast(fit, horizon=30)
Correct answer: forecast(fit, h=30)
forecast() from the forecast package generates h-step-ahead forecasts with confidence intervals directly from an ARIMA (or other) fitted model object.
Question 4: A survey researcher has a Likert-scale response coded 1-5 stored as integer but wants to treat it as an ordered factor for analysis. Which call creates an ordered factor correctly?
- as.factor(responses)
- factor(responses, levels=1:5, ordered=TRUE)
- ordered(responses, levels=c('SD','D','N','A','SA'))
- Both B and C are correct depending on whether numeric or label levels are used (Correct answer)
Correct answer: Both B and C are correct depending on whether numeric or label levels are used
Both factor() with ordered=TRUE and ordered() create ordered factors; the choice depends on whether numeric codes or character labels are stored.
Question 5: A data engineer needs to write a data frame to a PostgreSQL database from R without loading the entire table to check for conflicts. Which package and function perform efficient bulk inserts?
- write.csv(df, 'table.csv') then import via psql
- DBI::dbWriteTable(con, 'table_name', df, append=TRUE) (Correct answer)
- RODBC::sqlSave(con, df, tablename='table_name')
- RSQLite::dbWriteTable(con, 'table_name', df)
Correct answer: DBI::dbWriteTable(con, 'table_name', df, append=TRUE)
DBI::dbWriteTable() with append=TRUE efficiently bulk-inserts rows into an existing PostgreSQL table without overwriting it.
Question 6: A pharmaceutical researcher is performing survival analysis on time-to-event data with censoring. Which R package and function fit a Cox proportional hazards model?
- survival::coxph(Surv(time, event) ~ predictors, data=df) (Correct answer)
- lm(time ~ predictors, data=df)
- glm(event ~ predictors, family=binomial, data=df)
- MASS::survreg(Surv(time, event) ~ predictors, data=df)
Correct answer: survival::coxph(Surv(time, event) ~ predictors, data=df)
coxph() from the survival package fits Cox proportional hazards models; Surv() creates the survival object encoding time and censoring status.
Question 7: A data scientist profiles code and finds that a nested loop processing a 10,000-row data frame takes 45 seconds. After vectorizing with dplyr and data.table, it runs in 0.3 seconds. What R tool identified the bottleneck?
- system.time() wrapping the entire script
- Rprof() or profvis package to profile call-by-call execution time (Correct answer)
- traceback() to examine the call stack after an error
- benchmark() from the microbenchmark package comparing two expressions
Correct answer: Rprof() or profvis package to profile call-by-call execution time
Rprof() or profvis creates a call-level profiling report showing exactly which functions and lines consume the most time.
An urban planner uses R to analyze bike-share trip data and needs to calculate trip duration in minutes from two POSIXct columns (start_time and end_time).
Which expression is correct?