Skip to content
Module 01 of 1235 min readBeginner

Hello, R — vectors are everywhere

Why R is vector-first by default, the assignment arrow, and how to read R’s function help pages.

8%

Listen along

Read “Hello, R — vectors are everywhere” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Explain why R is vector-first by default and what that implies for everyday syntax
  • 02Use the <- assignment arrow and the ?function help system fluently
  • 03Recognise R's place relative to Python in the modern analyst toolkit
  • 04Run R in the browser via WebR and load the three pre-defined Kenyan datasets

R is the lingua franca of academic statistics, a major language in applied research and policy work, and the home of two ecosystems that no other language matches: the tidyverse for data manipulation and ggplot2 for graphics. If you do econometric research, R will be the tool you reach for at least half the time.

Vector-first by default

R was designed for statistics from the ground up. Its primary data type is the vector, not the scalar. Operations work element-wise without ceremony. This makes R unusually concise for the kinds of computations a statistician does — and unusually quirky for programmers coming from other languages.

r
x <- c(1, 2, 3, 4, 5)
x * 2 # 2 4 6 8 10
mean(x) # 3
x > 3 # FALSE FALSE FALSE TRUE TRUE

The assignment arrow

R uses <- (an arrow) for assignment, with = also accepted but considered less idiomatic. The arrow makes it visually obvious which way data is flowing. RStudio (the IDE most R users use) has a keyboard shortcut: Alt+- inserts <- with surrounding spaces.

Reading the help pages

Type ?function_name in the R console to open the help page. Every function in R has a structured help page with: arguments, value, examples, references. The examples at the bottom are usually the fastest way to figure out what a function does.

r
?mean
?lm
help("summary")

WebR — running R in your browser

The practice environment to the right of this course runs real R compiled to WebAssembly via WebR. The tidyverse and ggplot2 are pre-loaded. Three Kenyan datasets — bankrates, pension, mpesa — are loaded as data frames so you can practise on real data immediately.

R is unusual but consistent

Coming from Python, R will feel weird for a week. The vector-first nature, the <- arrow, the formula notation y ~ x, the [[]] vs [] distinction — they are all consistent with R's design but not with general programming language conventions. Embrace it; the conciseness is real.

Exercise

Print the string 'Hello, Kenya' (exact text, including the comma).

Key takeaways

  • R's primary data type is the vector — operations work elementwise without ceremony
  • Use <- for assignment (RStudio shortcut: Alt+-); = works but is less idiomatic
  • ?function_name opens the help page; examples at the bottom are usually the fastest way to figure it out
  • R remains the home of the tidyverse and ggplot2 — two ecosystems no other language matches

Further reading

  1. 01

    R for Data Science (2nd Edition)

    Hadley Wickham, Mine Çetinkaya-Rundel & Garrett Grolemund · O'Reilly · 2023Free online; the canonical starting point for modern R.

  2. 02

    Advanced R (2nd Edition)

    Hadley Wickham · CRC Press · 2019

  3. 03
Loading progress…
LeadAfrikPublic Economics Hub