Zum Inhalt springen
KodeTrail

Camp 1 · Schritt 2 von 12

Calculator and variables

Start with arithmetic, then store results — R's gentle on-ramp from math to data.

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

R began life as an interactive calculator for statisticians, and it still feels like one. That makes it a wonderfully low-pressure place to start.

R does math

RCloud-Run

Mostly familiar, with two R-isms:

  • ^ is power (2 ^ 10 = 1024)
  • %% is modulo, the remainder (17 %% 5 = 2)

(cat prints its arguments; the "\n" adds a line break.)

The assignment arrow

R's signature quirk: you store values with <-, an actual arrow pointing from value to name:

RCloud-Run

= also works, but <- is the deeply-ingrained R convention — you'll see it in virtually all R code, so we use it too. Read distance <- 13 as "distance gets 13."

Naming

total_steps <- 9500     # snake_case is common
avg.pace <- 4.2         # dots are legal in R names (unusual!)

R even allows dots in names (avg.pace) — a historical quirk found in few other languages. snake_case is the modern, safe default.

Checkpoint

What is R's conventional assignment operator?

Checkpoint

What does 17 %% 5 give in R?

What's next

The idea beneath all of R: the vector.