Vectors & Data Types
R’s building blocks — everything is a vector
📖 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.
- numeric — numbers:
c(1.5, 2.7, 3.0) - character — text:
c("Alice", "Bob") - logical — TRUE or FALSE:
c(TRUE, FALSE, TRUE) - integer — whole numbers:
c(1L, 2L, 3L) - factor — categorical with levels:
factor(c("A","B","A"))
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:
Exercise 1 — Exam Score Vector
Fill in the blanks to complete the exam score analysis. Replace each ___ with the correct value or function name.
mean() and sd() to the whole vector at once. scores > 85 returns a logical vector — use it inside [] to filter.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:
- Print only the California cities using logical indexing
- Print the city with the highest population using
which.max() - Print a formatted summary using
paste()orcat()
cities[in_california] filters cities where the logical vector is TRUE. cities[which.max(populations)] gives the city with the largest population.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):
- Find all even numbers (hint: use
%%for modulo) - Find numbers divisible by 3
- Find numbers that are BOTH even AND divisible by 3
- Calculate the sum of all odd numbers
nums[nums %% 2 == 0] finds even numbers. Combine conditions with &: nums[nums %% 2 == 0 & nums %% 3 == 0]. Use sum() on a filtered vector.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:
- Who is on honor roll? (print names)
- What is the average GPA by major? (use
tapply(gpas, majors, mean)) - Who has the highest GPA? (use
which.max()) - Print a formatted summary for each student using a for loop and
cat()
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 →