Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
Lesson 2 of 22
Understand atomic vectors, coercion, vectorized operations, missing values, and safe subsetting.
Motivation
Most R calculations begin with vectors. Knowing their type and length prevents silent coercion and indexing errors from spreading into later analysis.
Why this matters
Understand atomic vectors, coercion, vectorized operations, missing values, and safe subsetting.
Packages and data
Base R; no external data or packages.
Explanation 1
The live starter code is self-contained and uses only runner-compatible functions.
Explanation 2
Book-only outputs are labelled when the separate source dataset or advanced package was not supplied.
Explanation 3
R code, paths, function names, and formulas remain left-to-right in every locale.
Terminology
A one-dimensional collection whose elements share a type.
c(2, 4, 6)
Conversion to a common compatible type.
c(1, TRUE, '3') becomes character
A TRUE/FALSE vector selecting positions.
x[x > 4]
Notation and formulas
indexed return: 100 × cumulative product(1 + r)
Vectorized arithmetic applies the same rule element by element; recycling is safe only when intentional.
Worked example
Scenario
Which monthly values exceed 4%, and how does missingness change a mean?
R check
x <- c(2.1, 4.4, NA, 5.2, 3.7)
print(typeof(x))
print(x[x > 4 & !is.na(x)])
print(mean(x, na.rm = TRUE))The logical mask retains two high observations, while na.rm removes one unavailable value only for this calculation. Limitation: Removing NA does not explain why the value is missing or whether the available observations are representative.
Visual
Values above four are highlighted conceptually by taller bars.
R connection
Run the self-contained starter code in the protected STATLAB R runner. The code prints an auditable result and avoids network or unrestricted file access.
Live R Lab
Run a self-contained example, verify its output, and explain one limitation for vectors, types, and subsetting.
Ready to run
Common mistake
Negative indices exclude positions; mixing positive and negative indices is invalid. Never use na.rm = TRUE without discussing missingness.
STATLAB Tip
Check typeof(), length(), and sum(is.na(x)) before a vector enters a model.
Guided practice
Scenario
Create a logical mask for values from 2% through 4%, including endpoints, and verify how NA propagates.
Compare your result with the definition, units, and model assumptions—not only with a target number.
Exercises
Complete these without looking at the selected solutions. More than one defensible program may exist.
Explanation 1
Compare integer, double, logical, and character vectors with typeof().
Explanation 2
Select every second observation.
Explanation 3
Explain why c(1, '2') is character.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
Explanation 1
Use typeof(c(1L, 2L)) and typeof(c(1, 2)).
Explanation 2
x[seq(2, length(x), by = 2)]
Explanation 3
An atomic vector needs one type, so numeric 1 is coerced to text.
Chapter summary
Understand atomic vectors, coercion, vectorized operations, missing values, and safe subsetting.
Explanation 1
Atomic vectors have one common type.
Explanation 2
Vectorization avoids unnecessary loops.
Explanation 3
Subsets can use positions, names, or logical conditions.
Explanation 4
NA is a value requiring explicit treatment.
Terminology
inspect storage type
Use in Vectors, Types, and Subsetting.
construct indices
Use in Vectors, Types, and Subsetting.
detect missingness
Use in Vectors, Types, and Subsetting.
logical subset
Use in Vectors, Types, and Subsetting.
References and provenance
Safavi (2026), Chapter 2. Student notes: Mohammad Safavi, Ph.D., STATLAB Academy, Version 1.0.
Resource
The authoritative 125-page English PDF accompanies this native lesson.
DownloadReflection
Name the assumption, evidence you would seek, and how the recommendation might change.
Exit check
Reach 70% to complete the chapter. Explanations appear after submission.
Checkpoint
Question 1 of 2. Answered 0/2. Passing score: 70%.