R Basic Cheat Sheet
by Dipakk (Dipakk) via [Link]/67199/cs/16837/
Util Functions Arrays and Matrix (cont) Loops
getwd() gets the working 3D = array(1:24, dim = 3 dimensional for (variable in sequence){ Do for loop
directory c(4,3,2)) array something }
setwd("C:/file/p‐ sets the working mat = matrix(1:12, matrix while (condition){ Do while
ath") directory nrow=4, ncol=3) something } loop
data = read.csv(fil‐ opens file explorer to cbind(mat1,mat2) column bind if (condition){ Do something } ifelse
e.choose()) get data rbind(mat1,mat2) row bind else { Do something different } statement
ls() lists the variables
Vector Hypothesis
str(var) structure of the
variable num = c(1,2,3,4,5,6) numeric vector [Link](data) 1 sample t test
rm(var) removes the variable chr = c("aaa","bbb") character vector [Link](data1,data2) 2 sample t test
help.start() opens help log = c(TRUE,TR‐ logical vector [Link](pre,post,paire‐ paired sample t
UE,FALSE) d=TRUE) test
install.packages("p‐ installs the package
ackage name") mean(vec) mean [Link]st(data) Wilcox test
library("package makes the contents sd(vec) standard deviation [Link]st(data1,data2) correlation test
name") available to use chisq.test(data) Chi square test
var(vec) variance
detach("package detaches the shapiro.test(data) Shapiro test
range(vec) range
name") package
which.min(vec)/whi‐ position of the aov() ANOVA
ch.max(vec) min/max value
Strings
Visualization
rep(1:5,times=3) replicate elements
toString(x) helper function to
of vector qplot(data, line=T‐ produces quanti‐
produce a single
RUE,...) le-quantile plot
character string
DataFrame ggplot(data = NULL, initializes a
toupper()/to‐ converts text to upper/‐
df = data.frame(subjectID=‐ dataframe mapping = aes(), ...) ggplot object
lower() lower case
1:5,gender=c("M","‐ geom_bar() bar graph
substring(ch‐ retrieve or replaces the
F","M","M","F"),score=c(8,3,‐ coord_flip() flip x and y
r,n,n) substring of the
6,5,5)) coordinates
charachter
view(df) opens facet_grid() lay out panels in
paste (…, sep = Concatenate vectors
editor a grid
" ", collapse = after converting to
head(df)/tail(df) displays geom_density density plot
NULL) character
top/bottom
geom_hist histogram
n rows
Arrays and Matrix geom_point scatter plots
summary(df) returns
1D = array(1:24) 1 dimensional
descriptive
array
statistics of
2D = array(1:24, dim = 2 dimensional data
c(6,4)) array
Descriptive Statistics
rowMeans(data[]) row mean
rowSums(data[]) row sum
colMeans(data[]) column mean
colSums(data[]) column sum
By Dipakk (Dipakk) Published 29th August, 2018. Sponsored by [Link]
[Link]/dipakk/ Last updated 29th August, 2018. Everyone has a novel in them. Finish
Page 1 of 2. Yours!
[Link]
R Basic Cheat Sheet
by Dipakk (Dipakk) via [Link]/67199/cs/16837/
Probability
rbinom(n, size, prob) Binomial distri‐
bution
rpois(n,size) Poisson distribution
runif(n, min = 0, max Uniform distribution
= 1)
rnorm(n,mean,sd) Normal distribution
rexp(n) Exponential distri‐
bution
Statistics
summary(lm(y ~ x1 + x2 + multiple
x3, data=mydata)) regression
summary(glm(y ~ x1 + x2 + classific‐
x3, family="", data=mydata)) ation
cluster = kmeans(data) kmeans
cluster
analysis
By Dipakk (Dipakk) Published 29th August, 2018. Sponsored by [Link]
[Link]/dipakk/ Last updated 29th August, 2018. Everyone has a novel in them. Finish
Page 2 of 2. Yours!
[Link]