Style guides

Packages

Help

In an R command window type ?afunc to get help on a function named afunc

vignette("apackage") to display documentation for a package

str(obj) - Display the internals of an object

Data types

Vectors

v1 <- c(1, 2, 3, 4)
v2 <- c('Apple', 'Banana', 'Cherry', 'Date')
typeof(v2)  # character
v2[1]  # 1
v2[1:2]  # 1 2
v2[-1:-3]  # 4
v2[c(TRUE, FALSE, FALSE, FALSE)]  # 'Apple'
Function Description
typeof(vec) get the type of the elements
seq(1, 10) generate a vector which is a sequence of ints

Factors

f1 <- factor(c('DOG', 'CAT', 'MOUSE'))
f2 <- factor(c('SLOW', 'MEDIUM', 'FAST'), levels=c('SLOW', 'MEDIUM', 'FAST'))

Lists

l1 <- list(name='Will', age=39)
l1$name  # 'Will'
l1[[2]]  # 39
l1[2]  # $age 39

Dataframes

d1 <- data.frame(letters=c('a', 'b', 'c'), nums=c(1, 2, 3), syms=c('@', '?', '%'))
d1$letters   # extract whole letters vector
d1[c("letters", "syms")]  # dataframe with letters and syms
d1["letters"]  # dataframe with letters
d1[2,1]  # 'b' - [row, column]
d1[, 3]  # @ ? %
d1[-3]  # dataframe excluding syms

Matrices

m1 <- matrix(c(1, 2, 3, 4, 5, 6), nrow=2)  # 1 3 5
                                           # 2 4 6

m2 <- matrix(c(1, 2, 3, 4, 5, 6), ncol=2, byrow=TRUE)  # 1 2 3
                                                       # 4 5 6
                                                                                              
m3 <- matrix(c(1, 2, 3, 4, 5, 6), ncol=2)  # 1 4
                                           # 2 5
                                           # 3 6

m1[1, 2]  # 3
m1[1, ]   # 1 3 5
m1[, 1]   # 1 2

Arrays

Functions

complete.cases - returns a vector of booleans - FALSE if row value is NA

sum(complete.cases(nc$gained))

Data Handling

Loading and saving data

Function Description
save.image() Save session so it will be reloaded on restarting R
save(var1, var2, file='/tmp/some_file.rData') Save variables var1, var2 to file
load('some_file.rData') load whatever variables were stored in some_file.rData in the R working directory
rm(var1, var2) delete variables
ls() list all variables
rm(list=ls()) delete all variables
df <- read.csv('file.csv') load a dataframe from CSV file
write.csv(df, file='file.csv') Write dataframe to CSV

Exploring numeric variables

Function Description
summary(vec) Display 5 figure summary and mean (min, q1, median, mean, q3, max)
summary(c(vec1, vec3) side-by-side 6 figure summary for more multiple vars
mean(vec)  
min(vec)  
max(vec)  
IQR(vec) Q3 - Q1
range(vec) c(min, max)
diff(dev) generate differences of each consecutive pair in a vector
quantile(vec, prob=seq(0, 1, 0.1))  

Exploring variables

Function Description
table  
str  

Data Manipulation

scale - convert a vector to z-scores sample - take samples of a vector or numbers set.seed(123) - set seed used for PSNG as used in sample

Variables

rm - delete a variable

Normal Distribution

pnorm - gives percentile from critical value

p  <- pnorm(1.644854) # p = 0.95

qnorm - gives the critical value from percentile

z  <- qnorm(0.95) # z = 1.644854

Student’s T-distrubtion

pt - gives percentile from critical value and degrees of freedom

p  <- pt(1.644854, df = 50) # p = 0.9468633

qt - gives percentile from critical value and degrees of freedom

z  <- qt(0.9468633, df = 50) # z = 1.644854

F-distrubtion

pf - gives percentile from critical value and degrees of freedom

p  <- pf(10, 2, 100) # pf(10, 2, 100) 
© Will Robertson