R Programming Cheat Sheet
by Ann Santhosh via cheatography.com/67113/cs/16795/
Utility Functions Strings Vectors
getwd() Find the working dir. c("String1","String2") Create a string vector c(2, 4, 6) Numeric vector
setwd("C:/file/path") Set the working dir. toString(x) Convert to string c("one","two","thr") Character vector
ls() List variables noqutoe(string) Print string w/o quote c(TRUE,FALSE) Logical vector
rm(object) Delete object sprintf() Print text & var values rep(1:2,times=3) Repeat a vector
str(object) Displays internal cat() Concatenate & print rep(1:2,each=3) Repeat the elements
structure of R object
toupper(string) Convert to uppercase which.min() which.max()
help.start() Launch help console Index of the min Index of the max
tolower(string) Convert to lowercase
install.packages("pac Install package
substr(string,n,m) Extract substrings in a
kage_name") Data Frames
string from n to m
library(package_nam Load package strsplit(string," ") Split elements of string data.frame(x=1:2, Create data frame
e) y=c('a', 'b'))
paste(c("a","b"),"c") paste0(c("a","b"),"c")
detach(package:pack Remove package Concatenate vectors Concat w/o separator View(df_name) See full dataframe
age_name)
head(df_name) See first 6 rows
scan() Read data values Probability Distributions tail(df_name) See last 6 rows
rbinom(n, size, prob) Binomial df_name[cond, ] Row filter
Lists
rpois(n, lambda) Poisson df_name[c("column")] Column filter
list(x=1:5, y=c('a', 'b')) Create list
runif(n, min=0, max=1) Uniform df_name[cond1, ][ ,cond2]
is.list() as.list()
rnorm(n, mean=0, sd=1) Normal Row and Column filter
Check if the arg is a list Force the arg to list
rexp(n, rate=1) Exponential
lapply(list_name, function) Functions
Apply function over a list and return as list
function_name <- function(var){
sapply(list_name, function)
Do something
Return as suitable data structure(vector)
return(new_variable)}
args(function_name) - Arguments of func
body(function_name) - Body of func
By Ann Santhosh Published 30th August, 2018. Sponsored by Readability-Score.com
cheatography.com/ann- Last updated 30th August, 2018. Measure your website readability!
santhosh/ Page 1 of 2. https://readability-score.com
R Programming Cheat Sheet
by Ann Santhosh via cheatography.com/67113/cs/16795/
Flow Control Arrays Hypothesis Testing
If Statement - array(1:24, dim=c(4,3,2), dimnames=......) t.test(data, mu=3)
if (condition){ Create array with 4 rows, 3 cols and 2 groups One sample two-sided t-test
Do something t.test(data, mu=3, alternative='greater')
} else { Matrices One sample one-sided t-test
Do something different} m1 <- matrix(1:12, now=4, ncol=3, t.test(data1, data2, mu=0.5)
------------------------------- dimnames=....) Two sample two-sided t-test
Ifelse Statement - Create a matrix with 4 rows and 3 columns t.test(data1, data2, mu=0.5, alternative='less')
ifelse(condition, Do something, Do t(m) Transpose of matrix Two sample one-sided t-test
something different)
rbind(m1,m2) cbind(m1,m2) t.test(post_data, pre_data, paired=TRUE)
------------------------------- Combine by row Combine by column Paired test
Switch Statement -
The following applies to arrays also: wilcox.test(data, mu=8, alt='less')
switch("beta","alpha=1,beta=2,gamm Wilcoxen test
dimnames(m) dim(m) colnames(m)
a=3,4)
cor.test(data1, data2) chisq.test(data)
rownames(m) nrow(m) ncol(m)
Correlation test Chi-square test
Loops
Descriptive Statistics ks.test(data1, data2) shapiro.test(data)
For Loop - While Loop - If both are frm same distn Normality test
for (var in while summary(object) Summary of object
aov(data1~ data2) lm(data1~ data2)
sequence){Do (condition){ Do class(object) Find class of an R object ANOVA Regression
something} something} length(object) Get length of an object
quantile(x) Find quantiles
Visualizations
rowMeans(x)/ rowSums(x)/
barplot() plot() qqnorm() colMeans(x) colSums(x)
pie() plot(density()) qplot() table(x) Build a contingency table
mosaicplot() pairs() boxplot() describe(object) Description of object
hist() matplot() ggplot()
subset(x,cond) Create subsets
By Ann Santhosh Published 30th August, 2018. Sponsored by Readability-Score.com
cheatography.com/ann- Last updated 30th August, 2018. Measure your website readability!
santhosh/ Page 2 of 2. https://readability-score.com