R Quiz I: Practice Exam & Examiner Solution
Manual
Course: Quantitative Methods & Data Science in R | Fall 2026 | Value: 100 Points | Time: 75
Mins
OFFICIAL ADMINISTRATIVE RULES & INSTRUCTIONS:
1. Format Structure: The exam consists of three independent blocks, each centered on an R script.
2. The 5-Faulty-Lines Rule: Every R script contains EXACTLY FIVE faulty lines (syntax, argument types, or logic
errors).
3. Sequential Evaluation Rule: When identifying errors, assume that all earlier faulty lines have been
corrected.
4. Output & Interpretation Questions: Assume ALL FIVE faulty lines are perfectly corrected when answering
code output questions.
Block 1: Data Import, Subsetting, & Tidyverse Wrangling (35 Points)
Scenario Context: An analyst imports a Stata 17 dataset (survey_data.dta) containing individual demographic and
income data. The script cleans missing values, subsets numeric vectors, coerces data types, and constructs age
categories using dplyr.
1: library(haven)
2: survey_df <- read_dta("survey_data.dta", header = TRUE)
3: raw_age <- survey_df$age
4: valid_age_idx <- !is.na(raw_age)
5: clean_age <- raw_age[valid_age_idx]
6: young_sub <- survey_df$age[survey_df$age = 25]
7: raw_income <- survey_df$income
8: income_mean_val <- mean(raw_income)
9: print(paste("Unadjusted Mean Income:", income_mean_val))
10: clean_income <- ifelse(is.na(survey_df$income), mean(survey_df$income), survey_df$income)
11: survey_df$income_clean <- clean_income
12: edu_vector <- c("High School", "College", "Graduate", "College")
13: edu_factor <- factor(edu_vector, levels = c("High School", "College", "Graduate"))
14: filtered_df <- survey_df %>% filter(age >= 18, education == c("College", "Graduate"))
15: summary_by_edu <- tapply(survey_df$income_clean, survey_df$education, mean, na.rm = TRUE)
16: print(summary_by_edu)
17: survey_df <- survey_df %>% mutate(age_group = case_when(age < 30 = "Young", age >= 30 = "Adult"))
18: head(survey_df[, c("age", "age_group", "income_clean")])
Block 1 Questions
Quantitative Methods & Data Science in R Page 1
, Question 1.1: Error Identification (15 Points)
Identify the five faulty lines in Script 1. For each faulty line: (a) state the line number, (b) explain the error, and (c)
write the exact corrected line of code.
Question 1.2: Code Output & Behavior (10 Points)
Assume all five faulty lines have been corrected.
(a) Missing Values (5 Pts): If survey_df$income has 500 rows and 25 are NA, what value does income_mean_val
hold before vs. after line 8 is corrected? Explain na.rm.
(b) Vector Recycling (5 Pts): Contrast education == c("College", "Graduate") versus education %in% c("College",
"Graduate") for c("College", "College", "Graduate", "Graduate").
Question 1.3: Swirl & Base R Concepts (10 Points)
(a) Subsetting & Coercion (5 Pts): Explain atomic vector type coercion when concatenating c(10, "20", TRUE).
(b) Factor Levels (5 Pts): What happens to rows where education == "Graduate" if factor levels are defined as
c("High School", "College")?
Block 2: Aggregation, Functions, & Regression with Stargazer (35 Points)
Scenario Context: A researcher aggregates macroeconomic survey data by region, creates a custom function to
compute summary statistics, fits multiple linear regression models estimating wage determinants, and exports
formatted tables using stargazer.
1: library(dplyr)
2: library(stargazer)
3: region_summary <- survey %>% group_by(region) %>% summarize(region, mean_wage = mean(wage, na.rm = TRUE)
4: print(region_summary)
5: num_data <- survey[, c("wage", "experience", "education")]
6: calc_spread <- function(x) {
7: s_val <- sd(x, na.rm = TRUE)
8: return mean(x, na.rm = TRUE) + s_val
9: }
10: spread_results <- sapply(num_data, calc_spread)
11: print(spread_results)
12: model1 <- lm(wage ~ education + experience + gender, data = survey, weights = "pop")
13: print(summary(model1))
14: model2_data <- survey %>% filter(wage > 0)
15: model2 <- lm(log(wage) = education * experience, data = model2_data)
16: print(summary(model2))
17: stargazer_out <- stargazer(model1, model2, type = "text", title = "Wage Regression Analysis")
18: print(stargazer_out)
19: stargazer_combined <- stargazer(c(model1, model2), type = "text", out = "table1.txt")
20: cat("Block 2 Execution Complete.\n")
Block 2 Questions
Question 2.1: Error Identification (15 Points)
Identify the five faulty lines in Script 2. Provide line numbers, error explanations, and exact code corrections.
Question 2.2: Regression Interpretation & Stargazer (10 Points)
Assume all faulty lines have been corrected.
Quantitative Methods & Data Science in R Page 2
Manual
Course: Quantitative Methods & Data Science in R | Fall 2026 | Value: 100 Points | Time: 75
Mins
OFFICIAL ADMINISTRATIVE RULES & INSTRUCTIONS:
1. Format Structure: The exam consists of three independent blocks, each centered on an R script.
2. The 5-Faulty-Lines Rule: Every R script contains EXACTLY FIVE faulty lines (syntax, argument types, or logic
errors).
3. Sequential Evaluation Rule: When identifying errors, assume that all earlier faulty lines have been
corrected.
4. Output & Interpretation Questions: Assume ALL FIVE faulty lines are perfectly corrected when answering
code output questions.
Block 1: Data Import, Subsetting, & Tidyverse Wrangling (35 Points)
Scenario Context: An analyst imports a Stata 17 dataset (survey_data.dta) containing individual demographic and
income data. The script cleans missing values, subsets numeric vectors, coerces data types, and constructs age
categories using dplyr.
1: library(haven)
2: survey_df <- read_dta("survey_data.dta", header = TRUE)
3: raw_age <- survey_df$age
4: valid_age_idx <- !is.na(raw_age)
5: clean_age <- raw_age[valid_age_idx]
6: young_sub <- survey_df$age[survey_df$age = 25]
7: raw_income <- survey_df$income
8: income_mean_val <- mean(raw_income)
9: print(paste("Unadjusted Mean Income:", income_mean_val))
10: clean_income <- ifelse(is.na(survey_df$income), mean(survey_df$income), survey_df$income)
11: survey_df$income_clean <- clean_income
12: edu_vector <- c("High School", "College", "Graduate", "College")
13: edu_factor <- factor(edu_vector, levels = c("High School", "College", "Graduate"))
14: filtered_df <- survey_df %>% filter(age >= 18, education == c("College", "Graduate"))
15: summary_by_edu <- tapply(survey_df$income_clean, survey_df$education, mean, na.rm = TRUE)
16: print(summary_by_edu)
17: survey_df <- survey_df %>% mutate(age_group = case_when(age < 30 = "Young", age >= 30 = "Adult"))
18: head(survey_df[, c("age", "age_group", "income_clean")])
Block 1 Questions
Quantitative Methods & Data Science in R Page 1
, Question 1.1: Error Identification (15 Points)
Identify the five faulty lines in Script 1. For each faulty line: (a) state the line number, (b) explain the error, and (c)
write the exact corrected line of code.
Question 1.2: Code Output & Behavior (10 Points)
Assume all five faulty lines have been corrected.
(a) Missing Values (5 Pts): If survey_df$income has 500 rows and 25 are NA, what value does income_mean_val
hold before vs. after line 8 is corrected? Explain na.rm.
(b) Vector Recycling (5 Pts): Contrast education == c("College", "Graduate") versus education %in% c("College",
"Graduate") for c("College", "College", "Graduate", "Graduate").
Question 1.3: Swirl & Base R Concepts (10 Points)
(a) Subsetting & Coercion (5 Pts): Explain atomic vector type coercion when concatenating c(10, "20", TRUE).
(b) Factor Levels (5 Pts): What happens to rows where education == "Graduate" if factor levels are defined as
c("High School", "College")?
Block 2: Aggregation, Functions, & Regression with Stargazer (35 Points)
Scenario Context: A researcher aggregates macroeconomic survey data by region, creates a custom function to
compute summary statistics, fits multiple linear regression models estimating wage determinants, and exports
formatted tables using stargazer.
1: library(dplyr)
2: library(stargazer)
3: region_summary <- survey %>% group_by(region) %>% summarize(region, mean_wage = mean(wage, na.rm = TRUE)
4: print(region_summary)
5: num_data <- survey[, c("wage", "experience", "education")]
6: calc_spread <- function(x) {
7: s_val <- sd(x, na.rm = TRUE)
8: return mean(x, na.rm = TRUE) + s_val
9: }
10: spread_results <- sapply(num_data, calc_spread)
11: print(spread_results)
12: model1 <- lm(wage ~ education + experience + gender, data = survey, weights = "pop")
13: print(summary(model1))
14: model2_data <- survey %>% filter(wage > 0)
15: model2 <- lm(log(wage) = education * experience, data = model2_data)
16: print(summary(model2))
17: stargazer_out <- stargazer(model1, model2, type = "text", title = "Wage Regression Analysis")
18: print(stargazer_out)
19: stargazer_combined <- stargazer(c(model1, model2), type = "text", out = "table1.txt")
20: cat("Block 2 Execution Complete.\n")
Block 2 Questions
Question 2.1: Error Identification (15 Points)
Identify the five faulty lines in Script 2. Provide line numbers, error explanations, and exact code corrections.
Question 2.2: Regression Interpretation & Stargazer (10 Points)
Assume all faulty lines have been corrected.
Quantitative Methods & Data Science in R Page 2