R Reference Card
R Reference Card
c(a, b): concatenate, vectorize # comment char for (i1 in sequence1) dosomething : cycle
install.packages("package1"): install package package1 fun1 <- function(args1) dosomething : define function
is.na(x1): TRUE if x1 == NA if (condition1) ...else ...: single condition
library(package1): load package package1 ifelse(condition1, yes, no): vectorized condition
NA: missing value 3e3: 3 × 103 = 3000 (e-notation)
Logic and math
options(scipen=100): get rid of e-notation
<- or assign(): assign is.factor(obj1), is.atomic(obj1), is.data.frame(obj1):
q("no"): quit R, do not save environment check the type of object obj1
history(Inf); savehistory(file1): look on command history; mat1[mat1 > 0]: elements of mat1 which are positive
save history (not on macOS GUI) !<, &, |, ==: “not less”, “and”, “or”, “equal”
; used to separate commands :: used as package::command() cumsum(x1); diff(x1); prod(x1); sum(x1): vector math
Tab; Up; Ctrl+U: complete; repeat command; delete command round(x1): round
(not on macOS GUI) unique(x1): list unique elements of x1 (could be sparse) √
*, ^, sqrt(pi), abs(-3), log(1): multiplication, degree, π,
Help 3, natural logarithm
example(com1): run examples for command com1 x1 %in% x2: which elements of x1 are in x2
help(com1) or ?com1: help about command com1 which(logic1): indexes of all TRUE’s
help(package=rpart): help for the package, e.g. rpart
function1; methods(function2); getAnywhere(method2): look Descriptive statistics
on the function1 and 2 codes aggregate(...): pivot table
??"topic1": finds topic1 in all help files (slow!) apply(x1, n1, function): apply function to all rows (if n1
= 1) or columns (n1 = 2), output matrix
Entering and saving data colSums(mat1): calculate sums of every column
dir(...) and setwd(): list files in directory, go to another rev(x1), order(x1), scale(x1), sort(x1): reverse, sorting
read.table("file1", h=T, sep=";", as.is=T): read data into indexes, scale and center, (ascending) sort
data frame from file1 which has header and semicolon as sapply(); lapply(); do.call(); replicate(): vectorize
separator; do not convert variables into factors summary(x1); IQR(x1); fivenum(x1); mad(x1); max(x1);
scan("file1", what="char": read one series of character codes mean(x1); median(x1); min(x1); sd(x1); var(x1): de-
from disk into variable scriptive statistics
sink("file1", split=TRUE): output to file1 and to the termi- table(x1, x2): cross-tabulation
nal until sink() tapply(x1, list1, f1): apply function f1 to x1 grouping by
source("file1.r"): run commands from file file1.r list1
write.table(x1, "file1"): write object x1 to the file file1
Inferential statistics
Manage variables and objects chisq.test(tab1): χ2 -test for table tab1
1:3 or c(1, 2, 3): concatenate 1, 2, 3 into vector cor(df1): (Pearson) correlations between all columns of the data
as.data.frame(x1), as.matrix(x1): conversion frame
cbind(a1, b1, c1) or rbind(a1, b1, c1): join columns or cor.test(x1, x2): (Pearson) correlation test
rows into matrix ks.test(...); t.test(...), wilcox.test(...): other tests
cut(v1, 2, labels=c("small", "big")): split vector v1 in lm(...); glm(...); aov(...); anova(...): linear and non-
two intervals linear models, analyses of variation (ANOVA)
data.frame(v1, v2): list from same-length vectors v1 and v2 predict(model1): predict from model
df1$a1: variable (column) named a1 from data frame df1 lm(y ~ x + z, data=...): formula interface to the additive lin-
dimnames(mat1), or names(df1) and row.names(df1): names of ear model, y responses on two variables, x and z
rows and columns of mat1 or df1
droplevels(factor1): drop unused factor levels Multivariate statistics
grep("str1", x1): search str1 in x1 dist(...): distance calculation
gsub("str1", "str2", x1): replace str1 to the str2 in x1 cmdscale(...): metric multidimensional scaling (MDA)
head(df1): first rows of data frame hclust(...): hierarchical cluster analysis
length(v1), nrow(mat1), ncol(df1): sizes princomp(...); prcomp(...): principal component analyses
list1[[-5]]: all list elements except 5th (PCA)
ls(): list all active objects Plots
mat1[, 2:5] or mat1[, c(2, 3, 4, 5)]: columns from 2nd to
boxplot(...), dotchart(...), hist(...): useful plots
5th identify(...): reveal information from points using mouse
matrix(vector1, r1, c1): transform vector1 into matrix with legend("topleft", legend="..."): add legend to the top left
r1 rows and c1 columns, columnwise corner
merge(df1, df2): merge two data frames lines(...); points(...); text(...): add lines, then points,
paste("cow", "boy", sep=""): outputs "cowboy"
then text
rep(x1, n1): repeat vector x1 n1 times
pdf("file1.pdf"): draw into file1.pdf until dev.off()
sample(x1, n1): sample n1 elements from x1 without replace-
oldpar <- par(mfrow=c(2,1)): plots will be stacked until
ment
par(oldpar)
seq(n1, n2, n3): sequence from n1 to n2 by n3 steps
oldpar <- par(mar=c(0,0,0,0)): all plot margins set to zero
stack() and unstack(): convert from short to long form and
until par(oldpar)
back again
plot(.., cex=1|2): normal dot size, double dot size
str(obj1): structure of object obj1
plot(.., col=0|1|2|3): white, black, red, green color
t(mat1): rotate 90◦ matrix or data frame
plot(.., lty=0|1|2): no lines, straight line, dashed line
with(x1, ...): do something within x1
plot(.., type="p|l|s|n"): points, lines, stairs and no plot
Cycles, conditions and functions qqnorm(vec1); qqline(vec1): check normality
plot(.., pch=..): 1 ● 2 3 4 5 6 7 8 9 10 ● 11 12 13 ● 14 15
16 ● 17 18 19 ● 20 ● 21 ● 22 23 24 25 * * . a a ? ? 0