r basic cheatsheet
r basic cheatsheet
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 t.test(data) 1 sample t test
rm(var) removes the variable chr = c("aaa","bbb") character vector t.test(data1,data2) 2 sample t test
help.start() opens help log = c(TRUE,TR‐ logical vector t.test(pre,post,paire‐ paired sample t
library("package makes the contents sd(vec) standard deviation cor.test(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
Probability
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