Basics
x <- 42Assign value (preferred assignment operator)x = 42Assignment (also works; <- preferred by convention)x <<- 42Global assignment from within a function# commentCommentprint(x)Print valuecat("Hello", name, "\n")Concatenate and print - no newline by defaultclass(x)Type/class of objecttypeof(x)Internal storage typeis.numeric(x) / is.character(x) / is.logical(x)Type checksas.numeric(x) / as.character(x) / as.integer(x)Type coercionls()List variables in environmentrm(x)Remove variablerm(list = ls())Remove all variables?mean / help(mean)Get help for functiongetwd() / setwd("/path")Get/set working directoryData Types
42LInteger literal (L suffix)3.14Numeric (double)"hello"Character (string)TRUE / FALSE / T / FLogical values1+2iComplex numberNAMissing value - any typeNA_integer_ / NA_real_ / NA_character_Typed NA valuesNULLAbsence of value (different from NA)NaNNot a Number (0/0)Inf / -InfPositive/negative infinityis.na(x) / is.null(x) / is.nan(x)Test for special valuesfactor(c("a","b","a"))Factor - categorical variablefactor(x, levels = c("low","mid","high"), ordered = TRUE)Ordered factoras.Date("2024-01-15")Date objectSys.time()Current datetime (POSIXct)Vectors
c(1, 2, 3)Create vector - all elements same type1:10Integer sequence 1 to 10seq(0, 1, by = 0.1)Sequence with stepseq(0, 1, length.out = 11)Sequence of given lengthrep(0, times = 5)Repeat value 5 timesrep(c(1, 2), each = 3)Repeat each element 3 timesv[3]Access element at index 3 (1-based!)v[c(1, 3, 5)]Select elements by index vectorv[-1]Exclude element at index 1v[v > 2]Logical subsetting - elements matching conditionwhich(v > 2)Indices of elements meeting conditionlength(v)Number of elementsc(v, 99)Append to vectorv * 2Vectorized operation - applied to all elementsv + wElement-wise addition (recycling if lengths differ)sum(v) / prod(v) / cumsum(v)Sum, product, cumulative summean(v) / median(v) / var(v) / sd(v)Descriptive statisticsmin(v) / max(v) / range(v)Min, max, rangesort(v) / order(v)Sorted values / indices that would sortrev(v)Reverse vectorunique(v)Unique valuestable(v)Frequency tablenames(v) <- c("a", "b")Name vector elementsv["a"]Access by nameMatrices & Arrays
matrix(1:12, nrow = 3, ncol = 4)Create matrix - filled column-wise by defaultmatrix(1:12, nrow = 3, byrow = TRUE)Fill row-wisem[2, 3]Element at row 2, column 3m[1, ]First rowm[, 2]Second columndim(m)Dimensions: c(rows, cols)nrow(m) / ncol(m)Number of rows / columnst(m)Transpose matrixm %*% nMatrix multiplicationrbind(a, b)Stack matrices by rowscbind(a, b)Stack matrices by columnsapply(m, 1, sum)Apply function to rows (1) or cols (2)rowSums(m) / colSums(m) / rowMeans(m) / colMeans(m)Row/column aggregatesLists
list(name = "Alice", age = 30, scores = c(90, 85))Create named list - heterogeneouslst[[1]]Access first elementlst[["name"]]Access by namelst$nameAccess named element with $lst[1]Subset list - returns a listlength(lst)Number of elementsnames(lst)Get element nameslst$new <- "value"Add elementlst$name <- NULLRemove elementlapply(lst, fn)Apply function - returns listsapply(lst, fn)Apply function - simplify result to vector/matrixrapply(lst, fn, how = "unlist")Recursively apply to listdo.call(rbind, lst)Call function with list as argumentsData Frames
data.frame(name = c("Alice","Bob"), age = c(30, 25))Create data framedf[1, ]First rowdf[, "name"]Column by namedf$nameColumn with $ - returns vectordf[df$age > 25, ]Filter rows by conditionsubset(df, age > 25, select = c(name, age))Subset with column selectionnrow(df) / ncol(df) / dim(df)Dimensionshead(df, 10) / tail(df, 10)First/last N rowsstr(df)Structure - column types and previewsummary(df)Descriptive statistics per columndf$newcol <- df$a + df$bAdd computed columndf[, c("a", "b")]Select columnsdf[order(df$age), ]Sort by columnmerge(df1, df2, by = "id")Inner join on key columnmerge(df1, df2, by = "id", all.x = TRUE)Left joinrbind(df1, df2)Stack data framescbind(df1, df2)Bind columnsControl Flow & Functions
if (x > 0) { } else if (x < 0) { } else { }if / else if / elseifelse(v > 0, "pos", "neg")Vectorized if-elsefor (i in 1:10) { }for loop over sequencefor (item in lst) { }for loop over list or vectorwhile (x > 0) { x <- x - 1 }while looprepeat { if (done) break }repeat loop - must break manuallybreak / nextExit loop / skip to next iterationadd <- function(a, b = 0) { a + b }Function definition with defaultadd(a = 1, b = 2)Named argument callf <- function(...) { args <- list(...) }Variadic function - ... captures extra argsdo.call(f, list(1, 2, 3))Call function with argument list(function(x) x + 1)(5)Immediately-invoked function|> or %>%Native pipe (R 4.1+) and magrittr pipex |> sqrt() |> round(2)Chain with native pipeReduce("+", 1:5)Reduce vector with functionFilter(function(x) x > 2, v)Filter vector with functionMap(f, v1, v2)Apply f to paired elements - returns listString Manipulation
nchar(s)Number of characterspaste("a", "b", sep = "-")Concatenate with separatorpaste0("a", "b")Concatenate without separatorsprintf("%.2f%%", 3.14159)Formatted string (printf-style)toupper(s) / tolower(s)Change casetrimws(s)Trim whitespace from both endssubstr(s, 1, 3)Substring (start, stop - 1-based, inclusive)strsplit("a,b,c", ",")Split string - returns listgrepl("pattern", s)Returns TRUE if pattern foundgrep("pattern", v)Indices of matching elementsregmatches(s, regexpr("\d+", s))Extract first regex matchsub("old", "new", s)Replace first matchgsub("old", "new", s)Replace all matchesstartsWith(s, "Hi") / endsWith(s, "!")Prefix/suffix checkformatC(3.14, format = "f", digits = 2)Format number as stringI/O & Packages
read.csv("file.csv", header = TRUE)Read CSV fileread.table("file.txt", sep = "\t", header = TRUE)Read delimited text filereadLines("file.txt")Read text file as character vectorwrite.csv(df, "out.csv", row.names = FALSE)Write CSV filewriteLines(v, "out.txt")Write character vector to filesaveRDS(obj, "file.rds")Save single R objectobj <- readRDS("file.rds")Load single R objectsave(a, b, file = "data.RData")Save multiple objectsload("data.RData")Load saved objects into environmentinstall.packages("dplyr")Install package from CRANlibrary(dplyr)Load packagerequire(dplyr)Load package - returns FALSE instead of errorinstalled.packages()List installed packagesupdate.packages()Update all packagesdplyr::filter(df, age > 25)Qualify function with package name