R Cheatsheet
50 essential methods for coding interviews
Showing 50 of 50 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
c | c(...) | Combine values into a vector | O(n) | essential |
length | length(x) | Get the length of a vector or list | O(1) | essential |
seq | seq(from, to, by) or seq_len(n) | Generate a sequence of numbers | O(n) | essential |
rev | rev(x) | Reverse a vector | O(n) | essential |
unique | unique(x) | Remove duplicate values from a vector | O(n) | essential |
which | which(x) | Return indices where condition is TRUE | O(n) | essential |
%in% | x %in% table | Check if elements of x are in table | O(n * m) | essential |
sort | sort(x, decreasing = FALSE) | Sort a vector in ascending or descending order | O(n log n) | essential |
order | order(..., decreasing = FALSE) | Return indices that would sort the vector | O(n log n) | essential |
lapply | lapply(X, FUN, ...) | Apply function to each element, return list | O(n * f(x)) | essential |
sapply | sapply(X, FUN, ...) | Apply function to each element, simplify to vector/matrix | O(n * f(x)) | essential |
apply | apply(X, MARGIN, FUN) | Apply function over array margins (rows=1, cols=2) | O(n * f(x)) | essential |
paste | paste(..., sep = " ") or paste0(...) | Concatenate strings with separator | O(n) | essential |
nchar | nchar(x) | Count number of characters in string | O(n) | essential |
substr | substr(x, start, stop) | Extract substring by position (1-indexed) | O(n) | essential |
strsplit | strsplit(x, split) | Split string by delimiter into list of character vectors | O(n) | essential |
grep | grep(pattern, x) or grepl(pattern, x) | Find pattern matches (grep: indices, grepl: logical) | O(n * m) | essential |
gsub | gsub(pattern, replacement, x) | Replace all occurrences of pattern | O(n * m) | essential |
sum | sum(..., na.rm = FALSE) | Sum of vector elements | O(n) | essential |
mean | mean(x, na.rm = FALSE) | Arithmetic mean of vector | O(n) | essential |
max | max(..., na.rm = FALSE) | Maximum value in vector | O(n) | essential |
min | min(..., na.rm = FALSE) | Minimum value in vector | O(n) | essential |
abs | abs(x) | Absolute value | O(n) | essential |
cumsum | cumsum(x) / cumprod(x) / cummax(x) / cummin(x) | Cumulative sum/product/max/min | O(n) | essential |
table | table(...) | Build frequency table / count occurrences | O(n) | essential |
list | list(...) | Create a list (heterogeneous collection) | O(n) | essential |
is.na | is.na(x) / is.null(x) / is.numeric(x) | Test for NA, NULL, or type | O(n) | essential |
as.numeric | as.numeric(x) / as.character(x) / as.integer(x) | Type conversion functions | O(n) | essential |
any | any(..., na.rm = FALSE) | Check if any element is TRUE | O(n) | essential |
all | all(..., na.rm = FALSE) | Check if all elements are TRUE | O(n) | essential |
setdiff | setdiff(x, y) / union(x, y) / intersect(x, y) | Set operations: difference, union, intersection | O(n + m) | essential |
rep | rep(x, times) or rep(x, each = n) | Replicate elements of a vector | O(n * times) | common |
append | append(x, values, after = length(x)) | Add elements to a vector at a specified position | O(n) | common |
head | head(x, n = 6) | Return the first n elements | O(n) | common |
tail | tail(x, n = 6) | Return the last n elements | O(n) | common |
which.min | which.min(x) / which.max(x) | Return index of first minimum/maximum value | O(n) | common |
match | match(x, table) | Return positions of first matches of x in table | O(n * m) | common |
vapply | vapply(X, FUN, FUN.VALUE) | Apply function with specified return type | O(n * f(x)) | common |
mapply | mapply(FUN, ...) | Apply function to multiple vectors in parallel | O(n * f(x)) | common |
tapply | tapply(X, INDEX, FUN) | Apply function to groups defined by INDEX | O(n * f(x)) | common |
Reduce | Reduce(f, x, init) | Successively apply binary function to reduce vector | O(n) | common |
Filter | Filter(f, x) | Extract elements where function returns TRUE | O(n) | common |
toupper | toupper(x) / tolower(x) | Convert string to upper/lower case | O(n) | common |
sprintf | sprintf(fmt, ...) | Format strings with placeholders | O(n) | common |
sqrt | sqrt(x) | Square root | O(n) | common |
floor | floor(x) / ceiling(x) / round(x, digits) | Round down/up/to specified digits | O(n) | common |
diff | diff(x, lag = 1) | Lagged differences between consecutive elements | O(n) | common |
names | names(x) or names(x) <- value | Get or set names of vector/list elements | O(n) | common |
unlist | unlist(x, recursive = TRUE) | Flatten a list to a vector | O(n) | common |
duplicated | duplicated(x) | Identify duplicate values (TRUE for duplicates) | O(n) | common |