# Create the vectors and check their types:Week 2 In-Class Exercise: Vectors & Subsetting
GPH-GU 2182 · Statistical Programming in R
Work in pairs during class.
- 📥 Download
week-02-inclass.qmdto open and edit in RStudio (or use the</> Source Codebutton at the top right). - Render this Quarto document to HTML and push it to your GitHub repository by the end of class for participation credit.
1 Cycle 1: Vector Types & Implicit Coercion
1.1 Type Predictions and Verification
In the code chunk below, write R code to check the typeof() of each of the following expressions:
vec_a <- c(4, 7, FALSE)vec_b <- c(3L, 5.5, "control")vec_c <- c(TRUE, FALSE) + 5
1.2 Public Health Prevalence Calculation
A study recorded the HIV viral load copies/mL for 8 participants: 1200, 450, 0, 8900, 320, 0, 1500, 60.
- Create a numeric vector named
viral_loadwith these measurements. - Use a logical comparison and the
mean()function to calculate the proportion of patients with undetectable viral load (\text{viral load} = 0).
# Calculate undetectable proportion:2 Cycle 2: Creating Patterned Vectors
2.1 Dosage Schedules with seq()
An intravenous antibiotic is administered every 8 hours across a 72-hour hospital observation period.
Create a sequence named dose_hours starting at hour 0 up to hour 72 with increments of 8.
# Create dosage hours:2.2 Cohort Generation with rep()
A vaccine trial requires creating treatment labels for 12 participants: - 4 “Control” - 4 “Low-Dose” - 4 “High-Dose”
Generate this character vector using the rep() function and assign it to treatment_groups.
# Generate treatment labels:2.3 Extracting Multiples with Modulo %%
Given the sequence numbers <- 1:50, extract all numbers that are divisible by 7.
numbers <- 1:50
# Extract multiples of 7:3 Cycle 3: Subsetting & Mutating Vectors
3.1 Glucose Filtering and Cleaning
A metabolic health study collected fasting blood glucose levels (mg/dL) from 8 patients: glucose <- c(95, 142, 180, 88, 210, 115, -1, 104)
- Extract all values in the pre-diabetes range (100 \le \text{glucose} \le 125).
- The measurement
-1is a missing value code. Replace all negative values inglucosewithNA. - Extract all valid readings (\ge 126, excluding
NA) indicating diabetes.
glucose <- c(95, 142, 180, 88, 210, 115, -1, 104)
# 1. Pre-diabetes subset:
# 2. In-place replacement of negative values with NA:
# 3. Diabetes subset (>= 126, non-NA):4 Cycle 4: Sorting, Ranking & AI Verification
4.1 Patient Rankings
A clinic recorded wait times (minutes) for 5 emergency room patients: wait_times <- c(45, 12, 85, 30, 45)
- Sort the wait times in descending order (longest to shortest).
- Compute the ranks of each patient’s wait time using
rank(..., ties.method = "first").
wait_times <- c(45, 12, 85, 30, 45)
# 1. Descending sort:
# 2. First-appearance ranking:4.2 AI Corner: Spotting Vector Recycling Errors
An AI assistant was asked to calculate the Body Mass Index for 4 patients with heights (in meters) c(1.70, 1.85, 1.62, 1.78) and weights (in kg) c(65, 82).
The AI produced this code:
# AI Generated code:
heights <- c(1.70, 1.85, 1.62, 1.78)
weights <- c(65, 82)
bmi_calc <- weights / (heights^2)4.2.1 Questions:
- What did R do when computing
bmi_calc? Why is the result clinically invalid? - How would you write a defensive check in R to prevent vector recycling when calculating BMI?
(Write your explanation and defensive check below)