Learn Without Walls
← Back to R Practice Labs
Lab 1 of 10

Vectors & Data Types

R’s building blocks — everything is a vector

← Lab Home Lab 1 of 10 Lab 2: Data Frames →
⏳ Loading R... (first load takes ~15 seconds)

📖 Concept Recap

In R, everything is a vector — even a single number is a vector of length 1. Vectors are R’s most fundamental data structure.

Key functions: c() creates vectors, length() counts elements, class() checks type, str() shows structure, summary() gives stats. R is vectorized — operations apply to every element automatically.

👀 Worked Example

Study this code carefully before diving into the exercises:

# Creating vectors ages <- c(22, 25, 19, 31, 28) names_vec <- c("Alice", "Bob", "Carol", "David", "Eve") enrolled <- c(TRUE, TRUE, FALSE, TRUE, FALSE) # Exploring vectors print(ages) cat("Length:", length(ages), "\n") cat("Class:", class(ages), "\n") cat("Mean age:", mean(ages), "\n") # Vector operations — R is vectorized! ages_in_months <- ages * 12 print(ages_in_months) # Logical indexing adults <- ages[ages >= 21] cat("Adults:", adults, "\n")
✏️ Guided

Exercise 1 — Exam Score Vector

Fill in the blanks to complete the exam score analysis. Replace each ___ with the correct value or function name.

Output will appear here...
💡 Hint: R applies functions like mean() and sd() to the whole vector at once. scores > 85 returns a logical vector — use it inside [] to filter.
💪 Independent

Exercise 2 — City Data Vectors

Create a character vector of 5 city names, a numeric vector of their populations (in millions), and a logical vector for whether they are in California. Then:

  1. Print only the California cities using logical indexing
  2. Print the city with the highest population using which.max()
  3. Print a formatted summary using paste() or cat()
Output will appear here...
💡 Hint: cities[in_california] filters cities where the logical vector is TRUE. cities[which.max(populations)] gives the city with the largest population.
🔥 Challenge

Exercise 3 — Vector Operations Without Loops

Create a vector of numbers 1 to 20 using 1:20 or seq(). Then using vector operations only (no for loops):

  1. Find all even numbers (hint: use %% for modulo)
  2. Find numbers divisible by 3
  3. Find numbers that are BOTH even AND divisible by 3
  4. Calculate the sum of all odd numbers
Output will appear here...
💡 Hint: nums[nums %% 2 == 0] finds even numbers. Combine conditions with &: nums[nums %% 2 == 0 & nums %% 3 == 0]. Use sum() on a filtered vector.
🏆 Mini Project — Student Profile Dataset

Build a Student Dataset from Vectors

Build a complete student dataset using vectors. Create: student_names (6 students), gpas (numeric, realistic values), majors (character), years (integer 1–4), on_honor_roll (logical: TRUE if gpa ≥ 3.7).

Then answer these questions using only vector operations:

  1. Who is on honor roll? (print names)
  2. What is the average GPA by major? (use tapply(gpas, majors, mean))
  3. Who has the highest GPA? (use which.max())
  4. Print a formatted summary for each student using a for loop and cat()
Output will appear here...
💡 Hint: student_names[on_honor_roll] gives honor roll names. tapply(gpas, majors, mean) computes the mean GPA for each unique major automatically.

✅ Lab 1 Complete!

You’ve mastered R’s foundational data structure — the vector. Vectorized operations, logical indexing, and the apply family are the core of R’s power and elegance.

Continue to Lab 2: Data Frames →

← Lab Home Lab 1 of 10 Lab 2: Data Frames →