• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 2 out of 5 pages
Exam (elaborations)

R Quiz I Practice Exam & Study Guide (Fall 2026): Code-Reading, Debugging & Solutions

Document preview thumbnail
Preview 2 out of 5 pages

R Quiz I Practice Exam & Study Guide (Fall 2026): Code-Reading, Debugging & Solutions

Content preview

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

Document information

Uploaded on
October 29, 2026
Number of pages
5
Written in
2026/2027
Type
Exam (elaborations)
Contains
Questions & answers
$15.58

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
studysavvypro
3.5
(4)
Sold
27
Followers
0
Items
903
Last sold
3 days ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions