0% found this document useful (0 votes)
157 views2 pages

R Programming Cheat Sheet: by Via

This document provides a cheat sheet summary of key R programming concepts and functions organized into categories like utility functions, strings, vectors, data frames, lists, flow control, arrays, matrices, hypothesis testing, and visualizations. It lists common functions for file management, data manipulation, probability distributions, and more to serve as a quick reference guide for R users.

Uploaded by

Kimondo King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
157 views2 pages

R Programming Cheat Sheet: by Via

This document provides a cheat sheet summary of key R programming concepts and functions organized into categories like utility functions, strings, vectors, data frames, lists, flow control, arrays, matrices, hypothesis testing, and visualizations. It lists common functions for file management, data manipulation, probability distributions, and more to serve as a quick reference guide for R users.

Uploaded by

Kimondo King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

R Programming Cheat Sheet

by Ann Santhosh via cheatography.com/67113/cs/16795/

Utility Functions Strings Vectors

getw​d() 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. toSt​rin​g(x) Convert to string c("one","two","thr") Character vector

ls() List variables noqu​toe​(st​ring) Print string w/o quote c(TR​UE,​FAL​SE) Logical vector

rm(o​bje​ct) Delete object spri​ntf() Print text & var values rep(​1:2​,ti​mes​=3) Repeat a vector

str(​obj​ect) Displays internal cat() Concat​enate & print rep(​1:2​,ea​ch=3) Repeat the elements
structure of R object
toup​per​(st​ring) Convert to uppercase which.min() which.max()
help.st​art() Launch help console Index of the min Index of the max
tolo​wer​(st​ring) Convert to lowercase
inst​all.pa​cka​ges​("pa​c Install package
subs​tr(​str​ing​,n,m) Extract substrings in a
ka​ge_​nam​e") Data Frames
string from n to m
libr​ary​(pa​cka​ge_​nam Load package strs​pli​t(s​tri​ng,​" ") Split elements of string data.fr​ame​(x=1:2, Create data frame
e) y=c('a', 'b'))
paste(c("a","b"),"c") paste0(c("a","b"),"c")
deta​ch(​pac​kag​e:p​ack​ Remove package Concatenate vectors Concat w/o separator View​(df​_na​me) See full dataframe
age​_na​me)
head​(df​_na​me) See first 6 rows
scan() Read data values Probab​ility Distri​butions tail​(df​_na​me) See last 6 rows

rbin​om(n, size, prob) Binomial df_n​ame​[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_n​ame​[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) Expone​ntial
lapp​ly(​lis​t_name, function) Functions
Apply function over a list and return as list
function_name <- function(var){
sapp​ly(​lis​t_name, function)
Do something
Return as suitable data struct​ure​(ve​ctor)
return​(ne​w_v​ari​able)}

args​(fu​nct​ion​_na​me) - Arguments of func


body​(fu​nct​ion​_na​me) - 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 - arra​y(1:24, dim=c(​4,3,2), dimnam​es=......) t.te​st(​data, mu=3)


if (condi​tion){ Create array with 4 rows, 3 cols and 2 groups One sample two-sided t-test

Do something t.te​st(​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.te​st(​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​(co​ndi​tion, Do something, Do t(m) Transpose of matrix Two sample one-sided t-test
something different)
rbind(m1,m2) cbind(m1,m2) t.te​st(​pos​t_data, pre_data, paired=TRUE)
------​---​---​---​---​---​---​------- Combine by row Combine by column Paired test
Switch Statement -
The following applies to arrays also: wilc​ox.t​es​t(data, mu=8, alt='less')
switch​("be​ta",​"​alp​ha=​1,b​eta​=2,​gam​m Wilcoxen test
dimn​ame​s(m) dim(m) colnames(m)
a=3,4)
cor.test(data1, data2) chisq.test(data)
rown​ame​s(m) nrow(m) ncol(m)
Correlation test Chi-square test
Loops
Descri​ptive Statistics ks.t​est​(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)
sequen​ce){Do (condi​tion){ Do clas​s(o​bje​ct) Find class of an R object ANOVA Regression
someth​ing} someth​ing} leng​th(​obj​ect) Get length of an object

quan​til​e(x) Find quantiles


Visual​iza​tions
rowMeans(x)/ rowSums(x)/
barplot() plot() qqnorm() colMeans(x) colSums(x)
pie() plot(density()) qplot() tabl​e(x) Build a contin​gency table
mosaic​plot() pairs() boxplot() desc​rib​e(o​bje​ct) Descri​ption of object
hist() matplot() ggplot()
subs​et(​x,c​ond) 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

You might also like