توضیح 1
The live starter code is self-contained and uses only runner-compatible functions.
Loading
درس 2 از 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.
توضیح 1
The live starter code is self-contained and uses only runner-compatible functions.
توضیح 2
Book-only outputs are labelled when the separate source dataset or advanced package was not supplied.
توضیح 3
R code, paths, function names, and formulas remain left-to-right in every locale.
اصطلاحات
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]
نمادگذاری و فرمولها
indexed return: 100 × cumulative product(1 + r)
Vectorized arithmetic applies the same rule element by element; recycling is safe only when intentional.
مثال حلشده
سناریو
Which monthly values exceed 4%, and how does missingness change a mean?
بررسی با R
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
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.
تمرین هدایتشده
سناریو
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.
توضیح 1
Compare integer, double, logical, and character vectors with typeof().
توضیح 2
Select every second observation.
توضیح 3
Explain why c(1, '2') is character.
Selected solutions
These are compact solution routes. Confirm dimensions, units, and any changed modelling choices.
توضیح 1
Use typeof(c(1L, 2L)) and typeof(c(1, 2)).
توضیح 2
x[seq(2, length(x), by = 2)]
توضیح 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.
توضیح 1
Atomic vectors have one common type.
توضیح 2
Vectorization avoids unnecessary loops.
توضیح 3
Subsets can use positions, names, or logical conditions.
توضیح 4
NA is a value requiring explicit treatment.
اصطلاحات
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.
منبع
The authoritative 125-page English PDF accompanies this native lesson.
دریافتبازاندیشی
Name the assumption, evidence you would seek, and how the recommendation might change.
ارزیابی پایانی