R Introduction Flashcards
scalar, vector, matrix, array, list, data.frame
• scalar: variable with just one value
• vector: variable with several values of the same type
• Array: multidimensional collection of objects of the
same type (numbers or characters or factors)
• Matrix: special two dimensional array, as it is so often used, it has it’s own command
• Lists: ordered collections of objects not necessarily
of the same type (Access with rectangular brackets or $ notation)
• Data frame: a list of vectors
(Different vectors of one data.frame can have
different types; access for data frame columns also with $ or with brackets [])
mtr[1,1], mtr[1:10,], mt[,’col’],df$col, df$col[4], vec[4]
matrices:
mtr[1,2]: element 1, column 2
mtr[1:10,]: first 10 elements in every column (?)
mt[,’col’]: column named “col”
dataframes:
df$col same as df[[“col”]]: Column named “col”
df$col[4]: Column named “col” 4th element
vector:
vec[4]: 4th element
sorting, aggregating: sort, rev, order, aggregate, apply
sorting: gives back values rev: order: gives back indices aggregate: Aggregate: apply a function for a vector against each level of a categorical variable; zusammenfassung der Daten ohne Summary zu nutzen (ausgewählte Daten ) apply:
R data structures
name (dimension, type) scalar (0,1) vector (1,1) Array (>=2,1) Matrix (2,1) List (1,>=1) Data-Frame (2, >=1)
data reading: data, read.table, load, source
data: load certain dataset > data(HairEyeColor)
read.table: loading from FLAT Files (library(base), read.table)
load: • You can explicitly load and save your session with
save.image, save <> load
savehistory <> loadhistory
source: load the file inside R or execute the file from the terminal source('/path/to/sample.r')
daten manipulation, structuring: data.frame, matrix, cbind, rbind,
colnames, rownames, merge
data.frame: Data frame: a list of vectors, Different vectors of one data.frame can have
different types, access for data frame columns also with $ or with brackets []
matrix: x[,c(1,3)] = colum 1 and 3
cbind rbind, colnames rownames merge
control flow: if, else if, else; for, while
• decisions: if (if cond1 is TRUE do something …), else if (if cond2 is TRUE do something …), else (neither cond1 or cond2 are true do something else …)
• loops: for, while
• loop control: break, next
–> keywords
for (i in vec) { # do something } while (cond) { # do something } repeat { if (cond) { break } # do something }
Basic operators
- Mathematical: *, /, +, -, , ==, …
- Logical: & (and), | (or), %in% (in) , ! (not)…
- Own: ’%ni%’ FALSE
functions and their arguments: default, optional, special “…”, return
Arguments
• mandatory: without = sign
• optional: with = sign and default value
The magic ...” Argument
take any argument and delegate it my.barplot: light blue barplot always with a box around
Return in Functions
• need values outside function: use return
• just perform some sequence of operations no return
• R implicitly always returns the last statement