<- 10 X
Department of Mathematics
University of Manitoba*
Two ways:
or
First version is preferred by R purists.. I don’t really care
A very useful data structure, quite flexible and versatile
Empty list
Convenient for things like parameters
Then to access entries 3 and 4
Convenient: we could replicate the same list elements for “2023”, for instance
(Line 2: surrounded by ( ) so that the result appears)
The c()
command is ubiquitous in R
Used to make vectors, concatenate them, etc.
Since the first two entries are characters, the added entry is also a character. Contrary to lists, vectors have all entries of the same type
Very useful method to create a vector if you don’t know in advance how many entries it will have
Say
Then x+1
gives
i.e., adds 1 to all entries in the vector
The (from, to, by)
form is the default; others exist
It is possible (and often useful) to name vector entries
v1 v2 v3 v4 v5 v6
2.0 3.5 5.0 6.5 8.0 9.5
[,1] [,2]
[1,] 1 3
[2,] 2 4
[,1] [,2]
[1,] 5 6
[2,] 7 8
Here and elsewhere, naming the arguments (e.g., nr = 2
) allows to use arguments in any order
Probably the biggest annoyance in R compared to other languages !
A*B
is the Hadamard product \(A\circ B\) (denoted A.*B
in matlab), not the standard matrix multiplicationA %*% B
end
to access the last entry in a matrix/vector/list..length
(lists or vectors), nchar
(character chains), dim
(matrices.. careful, of course returns 2 values)Dimensions must be compatible
Can be useful sometimes
Not assigning a value returns the existing values, if any
By position
By name, if present, and combining
[,1] [,2] [,3]
[1,] 0.2398867 0.3167605 0.04903629
[2,] 0.8981800 0.7612056 0.60225418
runif(100)
: generate 100 uniformly distributed random numbers between the default min=0
and max=1
Note that indices are “local” to the result
D = matrix(data = runif(100), nc = 10)
rownames(D) = sprintf("R%d", 1:dim(D)[1])
colnames(D) = sprintf("C%d", 1:dim(D)[2])
(E = D[2:3, 5:7])
C5 C6 C7
R2 0.3275763 0.5880480 0.3150221
R3 0.8573982 0.3703588 0.7112473
Indices are “local” but names are those “extracted”
From the R documentation:
[data frames are] tightly coupled collections of variables which share many of the properties of matrices and of lists, used as the fundamental data structure by most of R’s modeling software
L3 <- LETTERS[1:3]
fac <- sample(L3, 8, replace = TRUE)
(df <- data.frame(x = 1, y = 1:8, fac = fac))
x y fac
1 1 1 B
2 1 2 A
3 1 3 A
4 1 4 B
5 1 5 C
6 1 6 B
7 1 7 A
8 1 8 A
which
functionGive the
TRUE
indices of a logical object, allowing for array indices
TRUE
indices: those indices for which a property is TRUE
which
is usefulwhich
can return array indices row col
[1,] 3 1
[2,] 3 3
[3,] 5 3
[,1] [,2] [,3] [,4] [,5]
[1,] 0.58 0.87 0.63 0.14 0.87
[2,] 0.11 0.69 0.43 0.33 0.26
[3,] Inf 0.13 Inf 0.46 0.80
[4,] 0.60 0.67 0.97 0.35 0.29
[5,] 0.36 0.57 Inf 0.30 0.58
is.type
, for whatever type
, is typically defined
is.array
, is.atomic
, is.character
, is.data.frame
, is.double
, is.function
, is.integer
, is.list
, is.logical
, is.matrix
, is.numeric
, is.object
, is.vector
Many packages also define specific types
Typically, if is.type
exists for type type
, then as.type
also exists
as.array
, as.data.frame
, as.list
, as.matrix
, as.numeric
, as.vector
Often: matrix \(\leftrightarrow\) data frame, list \(\leftrightarrow\) matrix
expand.grid
: makes a matrix with every combination of the values of the vectors p1
and p2