R Statistical computing
Light · Postcard
102 × 152 mm · PDF
Dark · Postcard
102 × 152 mm · PDF
Ink saver · Postcard
102 × 152 mm · PDF
Light · A4 landscape
297 × 210 mm · PDF
Dark · A4 landscape
297 × 210 mm · PDF
Ink saver · A4 landscape
297 × 210 mm · PDF
Light · A3 landscape
420 × 297 mm · PDF
Dark · A3 landscape
420 × 297 mm · PDF
Ink saver · A3 landscape
420 × 297 mm · PDF
Start here
Rx <- c(1, 2, 3)
df <- read.csv("<file>")
str(df)summary(df)df[df$<col> > 5, ]
?<topic>Rscript <file.R>
Concepts
- Vector one type
- List mixed types
- Data frame table of columns
- Result print or write
Running R
RRscript <file.R> [args]
Rscript -e '<expr>'
R --vanillaR --no-savecommandArgs(trailingOnly = TRUE)
q()source("<file.R>")
Help and packages
?<topic>??<term>example(<fn>)
apropos("<part>")
install.packages("<pkg>")
library(<pkg>)
<pkg>::<fn>
update.packages()
Vectors
c(1, 2, 3)1:10seq(0, 1, by = 0.25)
seq_len(<n>)
rep(<x>, times = 3)
length(<x>)
x[3]x[c(1, 3)]x[x > 5]x[-1]x["<name>"]
names(x) <- c("a", "b")
which(x > 5)%in%sort(x) / order(x)
unique(x)table(x)is.na(x)ifelse(<test>, <yes>, <no>)
paste("a", "b", sep = "-")
paste0("a", "b")
Data structures
list(a = 1, b = "x")
l$a / l[["a"]]
matrix(1:6, nrow = 2)
m[1, 2] / m[1, ]
factor(x, levels = c("lo", "hi"))
levels(f)class(x) / typeof(x)
str(x)as.numeric(x)as.Date("2026-10-14")
Data frames
data.frame(a = 1:3, b = c("x", "y",
"z"))
head(df, <n>)
tail(df, <n>)
nrow(df) / ncol(df)
names(df)summary(df)df$<col>df[<rows>, <cols>]
df[df$<col> > 5, ]
df[order(df$<col>), ]
df$<new> <- df$a * 2
subset(df, a > 5, select = c(a, b))
rbind(df1, df2)merge(df1, df2, by = "<key>")
aggregate(a ~ g, data = df, FUN = mean)
complete.cases(df)
na.omit(df)Control and functions
f <- function(x, y = 2) { x + y }
return(<value>)
if (<cond>) { } else { }
for (i in <seq>) { }
function(x) x^2\(x) x^2x |> f()stop("<msg>") /
warning("<msg>")
tryCatch(<expr>, error = function(e) <h>)
print(x) / cat("a", x, "\n")
Apply family
sapply(x, f)lapply(x, f)vapply(x, f, numeric(1))
apply(m, 1, sum)
tapply(x, g, mean)
do.call(rbind, <list>)
rowSums(m) / colMeans(m)
mean(x, na.rm = TRUE)
sd(x) / median(x) / quantile(x)
cor(x, y)round(x, 2)Import and export
read.csv("<file>")
write.csv(df, "<file>", row.names =
FALSE)
read.table("<file>", header = TRUE)
readLines("<file>")
writeLines(x, "<file>")
saveRDS(x, "<file>.rds")
readRDS("<file>.rds")
getwd() / setwd("<dir>")
list.files("<dir>")
Plotting
plot(x, y)hist(x)boxplot(<y> ~ <g>, data = df)
barplot(table(x))
lines(x, y) / points(x, y)
plot(x, y, main = "<t>", xlab =
"<x>", col = "red")
par(mfrow = c(1, 2))
pdf("<file>.pdf")
png("<file>.png")
dev.off()Recipes
# run: Rscript top.R data.csv 5args <- commandArgs(trailingOnly = TRUE)df <- read.csv(args[1])print(head(df, as.integer(args[2])))
df <- read.csv("sales.csv")res <- aggregate(amount ~ region, data = df, FUN = sum)res <- res[order(-res$amount), ]write.csv(res, "by_region.csv", row.names = FALSE)
png("hist.png", width = 800, height = 600)hist(df$amount, main = "Amounts", xlab = "EUR")dev.off()
Rscript -e 'x <- read.csv("data.csv"); print(summary(x))'



