Vectors Are Not Containers — They Are the Default
bookOne :: chapter-2Vectors Are Not Containers — They Are the Default
Most languages treat vectors as a convenience.
R treats them as reality.
If you think of a vector as a box holding many values, you will misuse R.
A vector is not a collection—it is the fundamental unit of computation.
There are no scalars in R.
There are only vectors of length one.
This is not a quirk.
It is the axis around which the language turns.
The Scalar Illusion
x <- 5
length(x)
The answer is not zero.
It is not special.
It is one.
Kesulitan dengan riset atau tugas akademik Anda?
Tim ahli Notivra siap mendampingi Anda memberikan solusi bimbingan dan dukungan akademik yang komprehensif.
This single fact explains why R behaves the way it does when you least expect it.
Vectorized Thinking Is Not Optimization
Consider:
1:5 + 10
Nothing is being looped explicitly.
No instruction says “repeat.”
R assumes that operations apply to entire vectors.
This is not syntactic sugar. It is a declaration of intent:
The analyst thinks in sets, not steps.
Why Loops Feel Awkward
You can write loops in R.
for (i in 1:5) {
print(i + 10)
}
But this is not how R wants to think.
Loops:
- hide structure
- obscure intent
- encourage state mutation
- break composability
They answer how instead of what.
R prefers you to state the transformation, not choreograph the movement.
Recycling Rules: A Feature, Not a Trap
c(1, 2, 3) + c(10, 20)
This line alarms beginners.
It should.
Recycling is powerful—and dangerous.
It exists because R assumes vector logic by default.
It trusts the analyst to know when lengths align meaningfully.
Silence here is not negligence.
It is expectation.
Control Flow Is the Exception
This is why if() behaves the way it does:
if (c(TRUE, FALSE)) {
"yes"
}
It fails—not because R is inconsistent, but because if() demands a single logical value.
When you want vector logic, you must say so:
ifelse(c(TRUE, FALSE), "yes", "no")
Explicitness is the price of power.
Why This Matters for Everything That Follows
Vector-first thinking explains:
- why
dplyrverbs operate column-wise - why modeling functions expect vectors
- why row-wise logic is a smell
- why performance scales without effort
Once this clicks, R stops feeling slow and starts feeling inevitable.
R does not ask you to tell it how many times to act.
It asks you to tell it what structure to act upon.
Think in vectors. Let loops retire.
Artikel Terkait
Structure Precedes Power
Power in R does not come from knowing more functions. It comes from knowing what you are holding before you touch it.
Lists: Where Complexity Lives
Lists are where R stops pretending to be easy and reveals what it actually is: a language built to represent complex, nested, uneven reality without flattening it.
Tables Are Coordinated Vectors, Not Grids
A data frame is nothing more—and nothing less—than a collection of vectors that: - share the same length - are aligned by position - are interpreted together