0% found this document useful (0 votes)
58 views7 pages

Stats 20 Cheat Sheet

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)
58 views7 pages

Stats 20 Cheat Sheet

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/ 7

CHAPTER I

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
, ,

Functions function name < -function


- carguments) E
log # taking log of ,
base) logarithm function # body of function
and outputs 3
function ) special type of object ; takes in input of argument
by executing set of commands
objects () & Is () see what's saved in workspace ; names of all objects in global environment

rmc) remove objects from workspace ; Um /list = Is()) removes everything from workspace

CHAPTER I

Essentials mode hierarchy :


logical < numeric a character
~ C) collects , concatenates combines values into a rector
,
length c) inputs a rector and outputs length of rector
mode() inputs an object and outputs type (or mode) of object

sequences + Repeated patterns


sea from , to , by =, length = > creates sequence of evenly spaced #s w/ specified start & end values

(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 ...,

length out 0 which generates integer(0) Sea len (8)


= 1: 8 -
=

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 :

: shorthand for default seat) w/unit increment (eX : bu 1 or -17 =

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

Vector Elements where X is a unmeric rector, is a rector , is a vector


Extracting & Assigning o v

[X] extract element struncated) 3th element is


positive indices
4th-element (S
· -> :

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 ,... .

X : Vector/list & FUN function to be applied :

4 FUN VALVE
"template" rector specifying output type expected from FrN function to output
:
.

4 ...: any optional arguments to be passed to FUN function


apply /CCTRUE , FALSE , NA) , iS TRUE logical (1)) ,

iSTRUE1) determines if input object identically equal to logical value TRUE

Basic Numeric summaryFunctions


- & (x) typical distance away from mean

var(x) square of standard deviations


range(x) minimum & maximum values of X (rector length 2)
diff(x) consecutive differences of X (Xi+ - X ; > TRUE)
soA(X) orders values of X (increasing order by default) Sort(X , decreasing =
orders by decreasing
firenmcx) 5 number summary of X (min , 91 , 92 , 93 , max
-

summary (x) 5- number summary & mean ; named rector


Technical subtleties
or unknown values ; means "not available" ; logical mode by default ; placeholder
NA represents missing
narm =
TRUE removes NA values from computations
NULL represents empth , undefined , or nonexistent value ; not rector object and has own special mode called NULL
NaN numeric value representing indeterminate form ; "not a number" ; illegal math

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"

logical (c ("T" "F" , "T")) as. numeric (c ("4" "O", "3")) F


ascharacter
Homework 2 as .

, ,

Type casting [1]TF T [13403


as. logical ,
as. numeric() ,
as character coeres/casts rector into one mode

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

Simondingementar Nrectorized functions

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

Boolean operators 1 &, I ,! rectorized Vector/


↓ (not) logical negation that outputs TRUE if input is FALSE and FALSE If input is TRUE ; @ beginning of logical statement
& land) compares 2/ + logical expressions of same length and outputs logical vector TRUE if both TRUE and FALSE
otherwise (both are FALSE or only one is TRUE) &&
1 Cor) compares 2/ + logical expressions of same length and outputs logical Vector TRUE if at least one is TRUE and FALSE only
if both are FALSE ; inclusive 11
& & 11 : not rectorized + expect length / logical rector ; use short-circuit evaluation : evaluate expressions from left to right
and only evaluate right expression if necessar

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 : ,

for (i in sea-len(6))[ var-fuz <-function (x) E


output [i] < rector [i] 12 03 E
if (shm (is ng(x)3
-

>
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) :

num-num + 6 numc-num + b Input has NA values


if (nm > 2013 =
if (num > 2013
[I] NA
break break
3 3 7) message function ex :
num = 25 3 3 1 .
message ("Computing variance unless NA's
2. computing variance unless NA
Basic Error Handling [I] NA
stop() stops execution of current expression & throws error message
warning c) throws warning message but does not stop execution of current expression
message() prints diagnostic messages ; updates user what function is doing
Homework 3 attr (A ,
"dim")
9 absolute value of elements of numeric vector
bJC) [1]23
ifelsel) rector form of it-else statement aCA ,
"dim") <- NULL
load() load objects from file in workspace ar (A , "dim") c -
< (2 , 3)
CHAPTER 5 [ ,
1]
matrices [1 ,
]

matrix srector , now


=, ncol =, byrow F takes in vector of values (data) and arranges them into matrix
;
=
&
& imnames = list (c() ,
c(s)) reads input rector and fills matrix by column scolumn-major
burow T : fills matrix in by now
=
order)
dimc) returns numeric vector of length 2 specifying input object's dimension cnrowx nool
nrow() ucol) input 2-D object and output #ofrows/columns respectively ; individual entries from
& dim?
abind() & rbinds) combine/bind columns and cours /respectively) into matrix
u chind needs same of vows 4 rbind needs same # of columns
attributes) see attributes of R object <eX : dims) numericrestar
attributes (A) c list (dim x) gives vector A dim attribute to convert rector into matrix (X
-
= :
length 2)
9 to c object desired attributes
, directin access /assign specific attribute
rownames & colname() & columns of a 2-D object curnames = NULL)
add names to rows
dimnanes() setting names add dimnames attribute to object 12 components
AC , ] matrix subsetting ; extract entry in its cow & jth column
blank matrix extracts all values in that dimension
% * % dot product <matrix unitiplication) (mxn) <hxp) mxp . =

diag () generates identify matrix of that size w/ number input; generates diagonal matrix w/ rector values

along diagonal for rector input ne are


solve() computes inverse of inputted matrix ; must besmare might not exist
Sinveible/housingular
:

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

XCi drop TRUE] remove levels when hurricanes [1 : 3 drop TRUE]


subsetting values
= =
, ,

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 "

Using R Packages & Looking at Data "package . datasets" "package Methods"


273 "Autoloads" "package base" . .

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
.

? or help) help on ; followed by function name


abuilt-in function
?? or Help Searuc) . don't know function name ; googling win R documentation ; search over all available help files
and return list of any documentatial that has alias , concept , title matching search term
libram chelp specific package already installed
=
"Package") receive help on

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)

head (dataset n) outputs first few values


,
of input object(elements , rows) [17432/
- -
n returns all but last
n values head (10 13 : head (10 : 1 ,
n = 8)

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 <
-

L: list "Name" c("L" "R" , "A") "Height"


=
,
= 1162 , 71 , 66) "weight" = c (115, 201 , 119)
,
, ,
Data Frames , class , Lists afidataframe "Income" <(4000 , NA , 2000)
=

data frame () inputs multiple vectors of same length and outputs data frame w/ each column
.

corresponding to restors (in order


n calrect/cere matrices into data frames data frame (matrix Name . Height weight incalle

& (i , j , drop T] extract entry


= ithrow and th column
in I Leslie 2 115 4008

drop = F preserves data structure when subsetting 2 Ron 7 201 NA

df([]] extracts columns/components by numeric index/name 3 April Gla 119 2000

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
,
-

mode() hav Rinerally stores object [13627166


n class : matrix ; mode : rector n class : of ; mode : list class : factor ; mode : numeric
list 1)
ordered collection of objects ; can be any other object in R
22] subset from lists ; can extract multiple components at a time and preserves structure of each component
Matrix
4 L "matrix"] list m matrix component inside preserve list structure and always output list - -

LC3] index is #component of list ; denote index of top level components in list ; extract individual components from list

single positive numeric value or single character for component name


4 L[C "matrix" 7) just matrix component
recursive list : list contains list coupment inside (C4]]([2]] = LC2C(412)]]
I
L comparent extract single component DataFrame' or "Data Frame"
names () outputs names of list components
names () -
strc) returns compact display of internal structure of input object
lapply (X FrN , ) apply function to each list component ; output is list ; Also works for data frames
,
...

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

output list w/ component


: length 1- vector
boutput : /rector of same length >1 + matrix
+ list Isame as lapply
boutput : list of different component length

CHAPTER & instructions to


( R) : doesn't contain object itself but contains recreate
Script/script file script/text
file w/ set of R commands
:
R
.

command to create input Robject(s) in text/script file on computer's hard


dump (list, file) store necessar
↑ & rive ; dumps R object into script file ; exports R command so to recreate object identically
source /file I reads in file name of R script and executes all commands inside file
; in global environment
btun ppl/computers and compartmentalize long program
u used to transfer code for functions + objects

into few . R files then combine at end


separate
R Data 1) no need to ru code again to recreate object
w/R objects inside ; contains object itself
:
binary file 10 .
,

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
.

read (SVC) import data from CSU files


text/csV file
.

(data table) from R into plain then read


table (X , file) export matrix/data frame object can
3 into Excel
write
. CSUC) wrapper function for CSV files
write
read table & read CJV read outside data in plain text files into R ;
- .
opposite of write table and write .
CSU

Print() script file to print results from a command


face
read CSU2 () import data tables from separated values
.
stisplit ("banana", split =
"na")
read . delims) import data tables from tab delimited data
(sep = "It")
"
[213] C1] "ba""
read. forf() import data tables from fixed width formatted files "an is
Stsplitbanana"split
=

read Spss() converts SPSs C sav] files

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

string character" "variable w/l/ + characters ; multiple characters


:

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 = "")
, ,

[1J "ha" "he" "no"


prints) generic printing
print (x) no quote (X) print (X , quote = FALSE) or
hoquotes) wout < 13 "Pawnee mies" rules
printing quotation marks [1] Pawnee
cat() concatenates multiple character rectors into single vector, adds specified separator, prints result (w/out quotes);
object ; can't save intel as
ca printsoutput tosewontactually returning paste (X,
"Eagleton drools" , 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

integer index or logical vector for match


y pattern character string to be matched in X
:
& X : input character rector where matches found
test-c("April" "and" "Andy" "love" , "champion")
, , ,

grep(pattm "a" test) [13 2 5


He "LEFT not
=

gsub(patter , replacement, x) finds + replaces patters in


,

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
-> -

High level Plot Functions where fat box plot


SY*
x a

plot() scatterplot ; input : factor >


- output : bar plot ; where both factors : stacked bar plot
scatter plot Mpe plot type cline dot plot on
plot
Pairs () text/point size
:
matrix , ceX :

Arguments List Pch : Point character (1-25) or letters


a breaks ↓ bins density langle : bar shading right : Whether values on boundary stor
breaks ,

free bins untained in left right bin


free hist free -, prob : = =
Firel free hist : free = F, prob = T
X lim X-axis range (vector 2) Xlab X-axis label main fitle :

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

uset horizontal : horizontal = T can use data argument w/ famula input


boxplot (weight ~ premie, data = births) NOT boxplot (weight, data-births)
table (births &Premies summarize factor barplotwith (data diamonds, = table (c+, color)
barplot() numeric rector-simple bar plot ; numeric matrix - stacked/segmented bar plot a
beside TRUE
side-by-side
=
:

Low Level Plot Functions


Points (c(X) , ((y) add pts to existing plot ; specify color, size , tupe ofpts ; argument
can be 2-column matrix

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
,
.

add = + to on top of another w hist function argument

CHAPTER 11

sample (X , size , replace F, prob


= =
NULL) choose subset of values / sample at random from fixed set of values
4 x numeric Scalar , then 1 :X

reta
~ pacwplacement nothing
n replications nexpr command : o
logical/character specifying to simplify to matrixarray
replicate (n = 5, sample (10 ,
size = 3))

< 13 < 23 < 3] C , 4] [ ,


5] sample 3 #s from 1 : 10 ,
5 times
column
, ,
repeat/iterations
,
4 Hot times to :

is 3 Hofalues in every column :


outoutfrom particular
instance
25 ] same thing as
sample-means >-numeric (3)
:
,

Sapply (sea-len (3) function (x) & , for (i in sea-len (3) [


sample-1- Sample (1 : 10 , size =
4) sample-1- Sample (1 : 10 , size =
4)
mean (sample i) -

sample-means [i] < -


mean (sample -
i)
3)
3
bar plot bar chart
,

discrete : finite possible values- >


p= sample ( , replace = +)
13
continuous
Fa
:
fix) runifcu min max =
=
for every xin 29 , b] ,
=o ,

generate n uniform #s blun min and max


rnorm in , mean ,
sa) generates landal sample of
sizen from normal dist. w/ mean m and So
& norm (X , mean , sa) function ; visualize namal density curre height
normal density ;

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

quantile(X, pubs , na . r> sample muntices

You might also like