ICT – codes voor R
Tab autocompletion of commands
Ctrl + Enter execute current/selected line(s)
Ctrl + ↑ autocomplete from history
A <- B of A=B store objects/values/string (B) in variables (A) (<- = operator)
A ask what is stored in variable A
TRUE of FALSE = logical
“Hi”, “1”, etc = character (“…”)
5, 1 e5 = numeric
class(…) check the type of object (logical, character, numeric)
is.<type>(…) check if it is a certain type of object (<insert type of object>)
as.<type>(…) convert the type of object (integer, character, factor, etc)
Vectors homogeneous
c(…, …, …) create/combine
…:… create numerical ranges (vb. 1:7)
A […] access elements using indices or names (vb. vec [2]) --->
A [[…]] access elements to remove attributes
A[-…] use negative indices to invert selection
A^2 squares vectors
A>3 compares vectors to 3 TRUE or FALSE
Lists heterogeneous
list1 <- list(num/char/lgl = …) create list (lgl = T of F)
list12 <- c(list1, list2) combine lists
list12[…] retain list structure
list12[[…]] access object inside list
list12$list1$char accessing by name
sapply(…, class) (list, function) ---------------------------->
names(my_list) <- c(“”, “”,) name lists
my_list[[2]][2] access entries inside object stored inside list
Matrices homogeneous
mat <- matrix(1:10, ncol = 3) create matrix function (ncol = aantal kolommen)
Surround expression with ‘(…)’ to immediately obtain the output ( A <- matrix(1:10, ncol = 3) )
mat[row, column] access certain row(s) and/or column(s) (vb. mat[2,3])
mat[c(rows), c(columns)] access multiple rows and/or columns (vb. mat[c(1,3), c(1,2)] or mat[2:4, 1:3])
Data frames heterogeneous
( data_frame <- data.frame(name = c(“John”, “Jane”, “Karen”), age = c(29, 15, 56), married = c(F, F, T) )
data_frame[data_people$married == TRUE, ] accessing using comparisons oid
mean(1:6) mean of numerical range
ln (x) gebruik log() gewone log = log10()
access dataframes with $ and the variable name/column name
ICT practical 1 (ma 2-9)
library(‘…’) check if package is already installed
install.packages(‘…’) install a package
library(…) load installed package
seq(…) generate a sequence (vb. seq(from, to, by) seq(0, 10, 0.1)
plot(x = …, y = …, ↓)
type = ‘…’ type of plot (vb. ‘p’ = points, ‘l’ = lines, etc)
main = “…” main title of plot
sub = “…” subtitle (at bottom)
xlab = “…” title for x-axis
ylab = “…” title for y-axis