Crash Courses · Basic
RStudio Setup, R Basics & Hello World
Install R and RStudio, understand the REPL and script execution model, and write your first data-aware R script.
12 min read+155 XP on completionCert: R Crash Course
Tap any word in the text below to start reading from there.
RStudio Setup, R Basics & Hello World
Why R?
R is the language of choice for statisticians, data scientists, and researchers worldwide. Unlike Python's general-purpose approach, R was built specifically for data analysis — statistics, visualization, and reproducible research are first-class citizens.
Key strengths:
- Best-in-class visualization: ggplot2 produces publication-quality graphics
- Statistical depth: thousands of statistical tests built in, with packages for every specialized method
- Data frames: tabular data handling is native, not an afterthought
- Reproducibility: R Markdown and Quarto combine code + narrative + output in one document
R is used at Google, Facebook, Airbnb, the FDA, and in every major academic journal for data analysis.
---
Installing R and RStudio
Step 1: Install R (the language)
- Go to https://cran.r-project.org
- Click your OS: Windows, macOS, or Linux
- Download and run the installer
- Verify: open a terminal and run
R --version
Step 2: Install RStudio (the IDE)
- Go to https://posit.co/download/rstudio-desktop/
- Download RStudio Desktop (Free)
- Install and open it
The RStudio Layout:
- Console (bottom-left): interactive R session — type code, see results
- Script Editor (top-left): write and save .R files
- Environment (top-right): all variables in memory
- Plots/Files/Packages (bottom-right): view charts, install packages
---
Running Code Two Ways
Interactive console — type directly, press Enter:
> 2 + 2
[1] 4
> "Hello, World!"
[1] "Hello, World!"The [1] means the first element of the result vector.
Script file — write a .R file, click Run or Source:
# This is a comment in R
x <- 10 # assign 10 to x
y <- 3 # assign 3 to y
z <- x + y # z = 13
cat("Sum:", z, "\n") # cat() for formatted output (no newline by default)
print(z) # print() outputs with [1]
message("Done!") # message() writes to stderr---
Basic Data Types
# Numeric (double by default)
x <- 42.5
class(x) # "numeric"
typeof(x) # "double"
# Integer (append L)
n <- 10L
class(n) # "integer"
# Character (strings)
name <- "Jordan"
class(name) # "character"
# Logical
is_active <- TRUE
class(is_active) # "logical"
# TRUE/FALSE (uppercase) or T/F (shorthand)
# Complex
z <- 3 + 2i
class(z) # "complex"
# NULL — absence of value
nothing <- NULL
is.null(nothing) # TRUE
# NA — missing value (different from NULL!)
missing_val <- NA
is.na(missing_val) # TRUE---
Arithmetic and String Operations
# Arithmetic
10 + 3 # 13
10 - 3 # 7
10 * 3 # 30
10 / 3 # 3.333...
10 %% 3 # 1 (modulo)
10 %/% 3 # 3 (integer division)
2 ^ 10 # 1024 (exponentiation — use ^ not **)
sqrt(144) # 12
abs(-5) # 5
# String operations
name <- "Jordan Morris"
nchar(name) # 13
toupper(name) # "JORDAN MORRIS"
tolower(name) # "jordan morris"
substr(name, 1, 6) # "Jordan" (1-indexed!)
gsub("Jordan", "Alice", name) # "Alice Morris"
paste("Hello", "World") # "Hello World" (space sep)
paste0("Hello", "World") # "HelloWorld" (no sep)
sprintf("Score: %.1f%%", 87.5) # "Score: 87.5%"---
Your First Complete R Script
# Temperature Analysis Script
# Run this with: source("temperature.R")
temps_c <- c(22, 18, 25, 30, 15, 28, 21) # Celsius readings
n <- length(temps_c)
# Vectorized conversion — applies to ALL elements at once
temps_f <- temps_c * 9/5 + 32
# Built-in statistics
cat("=== Temperature Analysis ===\n")
cat(sprintf("Readings: %d\n", n))
cat(sprintf("Celsius: Mean=%.1f, SD=%.1f, Range=[%.1f, %.1f]\n",
mean(temps_c), sd(temps_c), min(temps_c), max(temps_c)))
cat(sprintf("Fahrenheit: Mean=%.1f, SD=%.1f, Range=[%.1f, %.1f]\n",
mean(temps_f), sd(temps_f), min(temps_f), max(temps_f)))
# Which days were hot? (vectorized logical comparison)
hot_days <- which(temps_c > 25) # indices where temp > 25
cat(sprintf("Hot days (>25C): %s\n", paste(hot_days, collapse=", ")))
# Summary
summary(temps_c)Key insight: temps_c * 9/5 + 32 converts ALL 7 temperatures at once — no loop needed. This is R's vectorization in action.
Reading progress
0% read
In this module
R
RStudio
CRAN
Assignment operator
0%