Stats 20 Cheat Sheet
Stats 20 Cheat Sheet
Preliminaries
getwdc) outputs current working directory
setwrd() changes working directory to <"")
& () quit R Session
calculator computations
%% remainder after division
%
/% integer division ; how many times does number divide other number
evenly
>
assignment operator that saves objects , functions , anything into R's current workspace ; can contain letters,
numbers periods or underscores ; MUST start w/ a letter
, ,
rmc) remove objects from workspace ; Um /list = Is()) removes everything from workspace
CHAPTER I
(by) argument
↓ increase/decrease (lengths argument
increment of many values from interval n gives that
:
:
sea-len() inputs single length out argument & generates sequence of integers 1 , 2 , length out unless ...,
sea-along () inputs single along with argument & generates sequence of integers 1 , 2 , . . . , length (along With
sea-along (100) 1 seg-along (c ("f"=
,
"W" ,
" z")) =
1 3 :
rep(X , times) rector of repeated values ; times argument needs to be same length as input rector
4 (x) argument
:
rector of values to repeat /times) argument : times to repeat values in X rector
14 : 87 : 4th -
oth elements ; 28 4)
: :
· C-X] negative indices -> extract all elements EXCEPT ones w/ <-> indices [-4] : output all except 4th element
C-c(1 5) J : output all except 1st & 5th
, C-(1 : 4)] : output all except first four elements
02] subsetting w/ a blank index outputs everything (4] 1 43 -
assigns values to existing rector ; subset on left and assign to something on right 29 : 10]
= < (42 37)
-
· <]c v ,
-
numeric() , character() , logical c) creates empty vectors of specified length for respective modes ; default elements
""
are o
, , FALSE respectively
vectorization
rappIy(X , ,
) rectorize non-vectorized function ; applies function to each rector element
FUN FUN VALUE ,... .
4 FUN VALVE
"template" rector specifying output type expected from FrN function to output
:
.
Inf numeric
value representing infinity
a
typeof() outputs internal storage type of input object ; same as object mode except integer and double types both have
numeric modes type of (1 : 4) type o(32)
f "integer"= =
double : floating point is using double precision Chinam base 2 variation on scientific notation)
integer : whole number type of (pi) =
"double"
, ,
other Functions
lev() reverses order of elements in Vector
CHAPTER 3
Relational operators
= or equal to
less than == equal to ann ((1 : 10) > 7) = TRUE
> greater than or equal to
= != not equal to all ((1 : 10) > 0) =
TRUE
anyc) inputs logical rector and outputs TRUE if any of values is TRUE
identical (seq (1 , 10) ,
1 : 10) = TRUE
all() inputs logical rector and outputs TRUE if all values are TRUE identical 5) FALSE
(
213 415) , =
, ,
identical) inputs any 2 R objects and outputs TRUE if they're exactly identical
Sinteger 3L integer-stored version of number /41 is integer , 4 is double double integers
is na ()
. inputs object & outputs TRUE if corresponding elements are NA or NaN crectorized function
crectorized function
Logical Indexing where x is a logical rector o is a vector ; 2) x often redundant for subsetting
,
o[x] extracts every entry that corresponds to TRUE value in index rector satisfying some condition/criterion
run-times [run-times > 40] 515747 =
run-times [cCT , F)J returns every other value
TFT FT
of indices/positions of TRE Vale is
(x)inputslogicarectoranuptsume Vector
which
TF TFF T TT
other Functions
round #to round , # of digits to use) rounds numeric input to # of digits/decimal places
CHAPTER 4
Flow control
for sname in rector) d repeats set of commands fixed number of times ; performs literation of for each
loop entry in
# commands to repeat rector w/ name variable assigned to values in those entries
3 if (condition] & mus certain commands only if specified conditions met ; logical expression producing + /F
# commands when TRUE
3 else E
# commands when FALSE
3
holds crepeating if statement)
While <33 loop that repeats set of commands for as long as certain condition
9) logical condition expression evaluated b) if I condition executed commands
,
2) repeat until F condition which loop stops
wont built-in condition to exit loop until break statement
Repeat 1) E3 loop executes set of commands repeatedly
so break executed
break immediately exits/breaks out of loop ; usually inside of if c) statement only if certain
condition) [
condition (exit condition satisfied if
break
1) for output <-numeric (6) rector - - c (4 , 8 15 , 167 5) stop function ex:
loop ex : ,
>
3
.
↓
stop /" Input has NA values")
output =
1664225 256
2) if statement ex x-1 y if (X > 1)E if15 < 03
3
=
3
:
else if (x> 13 E X
"hello" sum(/X-mean(x)) 12 Klength (x) 1) -
3 else [
Y X 3 else E
Bar-fu2 (incomplete -nums)
-
I
3 else E X+ /
"goodbye"
X +1
yc
-
3 3
y= 2 3
3) while loop ex : numc-I numc-1
.
2 Error in var-fut (incomplete-nums):
while (numc = 20) E while (num + 5 < 203[ Input has NA values
num--nnm + num--nnm +
Yum = 25
num = 19 6) waring function ex :
4) repeat loop ex :
numc-1 numc-
↓
warning ("input has NA values")
repent While (TRVE) E .
2
warning in var-fuz (incomplete-nums) :
diag () generates identify matrix of that size w/ number input; generates diagonal matrix w/ rector values
apply <X ,
MARGIN , FUN , ... ) applies function to rows/columns cmargins) of matrices [1] = nw , [2] = col
X :
matrix/data frame MARGIN : rector giving dim/subscript(s) over which function applied over
FUN : function to be applied ...: any optional arguments passed to FrN functial
colMeans() & cowMean's compute mean of every row/column in matrix/data frame
integer (o) empty integer vector
. .
pass optional arguments to functions used inside main function (from outside function into inner plot call)
as. integer (groups (group) (1) < "p"
CHAPTER 6 groups- ("s", "T""s""T""T's levels -
[1312122
group (1) p + p + +
factor (groups <ICT C + T
Factors Levels : control treatment Levels : p +
ordered
factor ( ,
levels =, TorF) & as factor () creates / corres rector into factor ,
represents categoncal data ; stored as numeric
how levels of group arecoded/stored ; coekes down to integer values table (group)
as integer()
levels () access levels attribute of factor rector stored as characters group
hurricanes [1 : 3] placebo treatment
levels () - change factor labels nlevels (groups (1]2
Z 3
[13312
nlevels) returns number of levels in factor
table() output frequency table that summarizes factor levels : 12345 factor churricanes [1 : 3]) OR
tapply <X , INDEX , FUN , . . . , simplify = T) apply function to subsets of Vector (1) 31L
Levels 1 2 3
numeric/logical rector to split ↓ INDEX : factor/list of factors
identifying subsets
:
4 X :
4 FUN : functial to be applied . ..: any optional arguments passed to FUN
b
tapply (#-orders , food , sum)
·
simplify : logical value specifying to simplify output to matrix/away compute total # orders for each food item
tapply <pressure , list <category , year) , mean)
Homework 4 compute mean pressure for each category/year combination
t() transpose of matrix/data frame A = 2x3 - AT =
3X2
cor1) input 2 rector arguments of same length and output correlation crefficient ston them
row) returns matrix of integers indicating row number (ith element equal to i
col() returns matrix of integers indicating column number (ijth element equal to j)
missing () returns TRUE if function's formal argument missing/not specified and has no default value
order) inputs rector and outputs indices of input vector that returns sorted values
rank() inputs rector and outputs relative lank of each element
search (2
CHAPTER 7
GE"pageMputils "
library () load + access installed package in R Session ; libmm is directory of installed packages on computer libram (MASS)
search() outputs R's current search path ; see what packages currently loaded
install packages () install packages from CRAN ; package name in single or double quotations ; only need to be installedace
.
data() loads datasets from available package currently in search path and saves copy to workplace
package argument lists available datasets in specific package tail (10 : 1, u = =
6)
tail (dataset , as outputs last few values of input object 2131098765 213 109
3-u returns all but first a values parks of <-data frame <
-
data frame () inputs multiple vectors of same length and outputs data frame w/ each column
.
of $ column-name can also add new column of same length to existing of parks-df 2 , 2] Parks-df2 , "Height"]
with() reference variable names inside of wont brackets or operator parks-df [C "Height" 7] pans of [[2]] -
class() outputs objects class ; how R will present output when object called parks of $Height with (parks-of Heights
,
-
LC3] index is #component of list ; denote index of top level components in list ; extract individual components from list
4X : list 4 FrNifunction to be applied - ...: any optical arguments to be passed to Fun function
buseful when result from each component man have different length/dim/class
sapply c) internally calls lapply) to apply each list campment-> simplifies output function to
save (ol file] save specific R objects /listed as separate arguments) from workspace into RData file r
,
02 ,
save image (file) save all objects in workspace ; make workspace image into file name to create
.
load() reads in file name of RData file and loads contained objects into workspace L
read table 1) import data stored in table format (plain text/tab delimited file) ; output : data frame
4
file :
name + location/ URL header = F : 1st line/roW contains variables names or not
& Sep =
""
: separate values win line of data table
count fields () counts # of fields (separated by sep value) in each line of data table
·
read CV plain text files where separators are cammas ; comma separated values
.
sas
.
from foreign
.
read atal) .
converts Stata ( dta) files .
read xports)
.
converts transport ( Xport) files from .
package
CHAPTER 9
characters
character : symbols that represent text/words ; created using either single or double quotation marks
paste . . . , Sep , collapse NULL) inputs 1/ + Robjects , converts to character, concatenates/pastes them to form
= =
1/ + character strings
4 ...: any of objects beep : separator bun characters after pasting collapse characters to separate result :
"" collapse="
paste hia") a
sep
= and paste ("h", c("a", "e", "O") Sep = "")
, ,
,
"
Pawnee mles , Eagleton drools C1] "Pawneemles , Eagleton drools"
I save to external file using file argument
format() formats for "pretty" printing ; corres numeric rectors into characters
Robject
width : (min) width of strings producedh frim : padding w/ spaces or not (TRUE)
4 justify : how padding takes place for strings ("left" , "right", "centre" "none") ~
digits : # of significant digits
,
↑ usmall : min # of digits to right of decimal place to include scientific : T/F
nchar ("Pawnee rles", "Eagleton drools") nchar /NA , keepNA= F)
Basic String Manipulation to lower (y) or casefold (y)
[1]12 15 [I] 2
nclarc) returns # of characters [I] "Pawnee oles" "eagleton drools"
folower () & touppers converts to lowercase/uppercase toupperly) or casefold (y, upper = 5)
casefold() mapper for fo lower () and touppers) 213 "PAWNEE RULES" "EAGLETON PROOLS"
YI
"
chartrcold new) translates characters one-to-one
, charte /"deo" "# ?! , ,
"
substrc) extracts substrings of character vector [1J "PAWn ?? M1 ? " "Engl ? +In dr !! Is
from split
strsplit()splits strings into substrings based on pattern "I")
list object stsplit (x, split =
Stop 5) 3 As always a
substrySta
=
,
(inclusive) (213]
(1] "Pawnee un" "es"
nulist() removes list structure ; turns into single character rector and concatenates all values together
patter matching
glep /pattern grep1)) searches for matches to patter in input character rector ; returns numeric vector/
, x) or
need
to be one-to-me) i more flexible than charts
g sub (patter "a" , replacement "X" , test) =
regular expression/regex : set of symbols that describe
=
< 17 "April" "Xnd" "Andy" "love" "chxmpio" text pattern C ! ?:i ] 22 : punct : ]] .
Homework 6
times that repeats entries of times
strep (X , times) inputs character rector X and integer rector
X
Jet seed()
.
Set to value to make work reproducible
CHAPTER 10 y numeric
Yux explanatory response
vector. side-by-side
-> -
Y lim y-axis range (rector 2) Ylab y-axis label col : bar colors
; also accepts numeric values
boxplot) unmeric rector inputeboxplot ; numeric matrix/data frame input-side-by-side box plots
or data frame
lines 1) add connected line segments to existing plot ; connect specified cominates by straight line segments
4
Iy (line type) n Iwd cline width) lines (density (trees $Girth)) smooth density curve for hist
text (X, Y , labels) add text locator (n) click n pts and outputs coordinates
expression's type math expressions in R
abline (a , b) adds straight line y
= a + bx
; rectorized
h=
y horizontal line
n 4 v = X :
vertical line
1m1) linear model ; inputs Yex and outouts Im object Imcirth-Height , data=trees
legend (X , legend) Xandy coordinates specify
4 cex : entire
y ,
legend size b
pt . CeX :
point size for legend
top-left comer
entries 4bM : box draw/ not be indest
Iy
hist
box 1 Ind
superimpose
,
.
CHAPTER 11
reta
~ pacwplacement nothing
n replications nexpr command : o
logical/character specifying to simplify to matrixarray
replicate (n = 5, sample (10 ,
size = 3))
curve <draw (x
,
mean , sa)
are/exper ,
form , to , n
,
... ) high level draws a
curve corresponding to function over specified internal
inverses
know (g , mean so lawer fail) off prob from nnmal dist less than or equal to r
, , ,
: ; I
anoumcp , mean , sa) quantile ; inputs probability value and computes quantile ; location