Zum Inhalt springen
KodeTrail

Camp 1 · Schritt 3 von 12

Vectors

R's fundamental data structure — work on whole collections at once, the way R was built to.

14 Min.+50 XPAuf Englisch angezeigt — Übersetzung ist unterwegs

Here's R's biggest surprise, and its biggest strength: there are no single values in R — only vectors. Even x <- 5 is secretly a vector of length one. Once this clicks, R's whole design makes sense.

Building a vector

The c() function ("combine") builds a vector:

RCloud-Run

Vectorized operations: the magic

Operate on a vector and R applies it to every element at once — no loop required:

RCloud-Run

prices * 2 doubles all four values in one stroke. In most languages this needs a loop; in R it's the default. This "vectorized" thinking is the heart of writing good R — you describe operations on whole datasets, not element-by-element steps.

Reaching in with []

Access elements by position — and note R counts from 1, not 0:

RCloud-Run
Checkpoint

If v <- c(2, 4, 6), what does v * 3 produce?

Checkpoint

For days <- c("Mon", "Tue", "Wed"), what is days[1]?