Context
Matices and arrays are an extension on R's atomic vecotrs.
Atomic vectors contain values (not objects).
They hold a contiguous selt of values, all of which are the same basic type. There are six types of atomic vecotr: logical, integer, numeric, complex, caracter and raw.
Importantly: atomic vectors have no dimension attribute.
Matrices and arrays are effectively vectors with a dimension attribute.
Matrices are two-dimensional(tabular) objects, containing values all of the same type (unlike data frames).
Arrays are multi-dimensional objects(typically with three plus dimensions), with values all of the same type.
Matrix versus data.frame
In a matrix, every column, and every cell is of the same basic atomic type.
In a data.frame each column can be of a different type(eg. numeric, character, factor). Data frames are best with messy data, and for variables of mixed models.
Matrix creation
## generalCase <- matrix(data=NA, nrow=1, ncol=1, byrow=FALSE, dimnames=NULL)
> M <- matrix(c(2,1,3,4,5,6), nrow=3, byrow=TRUE); M
[,1] [,2]
[1,] 2 1
[2,] 3 4
[3,] 5 6
> b <- matrix(c(0, -1, 4)); b
[,1]
[1,] 0
[2,] -1
[3,] 4
> I <- diag(3); I
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1
> D <- diag(c(1,2,3)); D
[,1] [,2] [,3]
[1,] 1 0 0
[2,] 0 2 0
[3,] 0 0 3
> d <- diag(M); d
[1] 2 4
Basic information about a matrix
> dim(M)
[1] 3 2
> class(M)
[1] "matrix"
> is.matrix(M)
[1] TRUE
> is.array(M)
[1] TRUE
> is.atomic(M)
[1] TRUE
> is.vector(M)
[1] FALSE
> is.list(M)
[1] FALSE
> is.factor(M)
[1] FALSE
> is.recursive(M)
[1] FALSE
> nrow(M)
[1] 3
> ncol(M)
[1] 2
> length(M)
[1] 6
> rownames(M)
NULL
> colnames(M)
NULL
Matrix manipulation
> M <- matrix(c(2,1,3,4,5,6), nrow=3, byrow=TRUE); M
[,1] [,2]
[1,] 2 1
[2,] 3 4
[3,] 5 6
> N <- matrix(c(6,5,4,3,2,1), nrow=3, byrow=TRUE); N
[,1] [,2]
[1,] 6 5
[2,] 4 3
[3,] 2 1
> newM <- cbind(M, N); newM
[,1] [,2] [,3] [,4]
[1,] 2 1 6 5
[2,] 3 4 4 3
[3,] 5 6 2 1
> newM <- rbind(M, N); newM
[,1] [,2]
[1,] 2 1
[2,] 3 4
[3,] 5 6
[4,] 6 5
[5,] 4 3
[6,] 2 1
> v <- c(M); v
[1] 2 3 5 1 4 6
> df <- data.frame(M); df
X1 X2
1 2 1
2 3 4
3 5 6
Matrix multiplication
> M
[,1] [,2]
[1,] 2 1
[2,] 3 4
[3,] 5 6
> N
[,1] [,2]
[1,] 6 5
[2,] 4 3
[3,] 2 1
> InnerProduct <- M %*% t(N); InnerProduct
[,1] [,2] [,3]
[1,] 17 11 5
[2,] 38 24 10
[3,] 60 38 16
> OuterProduct <- M %o% N; OuterProduct
, , 1, 1
[,1] [,2]
[1,] 12 6
[2,] 18 24
[3,] 30 36
, , 2, 1
[,1] [,2]
[1,] 8 4
[2,] 12 16
[3,] 20 24
, , 3, 1
[,1] [,2]
[1,] 4 2
[2,] 6 8
[3,] 10 12
, , 1, 2
[,1] [,2]
[1,] 10 5
[2,] 15 20
[3,] 25 30
, , 2, 2
[,1] [,2]
[1,] 6 3
[2,] 9 12
[3,] 15 18
, , 3, 2
[,1] [,2]
[1,] 2 1
[2,] 3 4
[3,] 5 6
> CrossProduct <- crossprod(M, N); CrossProduct
[,1] [,2]
[1,] 34 24
[2,] 34 23
> M * N
[,1] [,2]
[1,] 12 5
[2,] 12 12
[3,] 10 6
Matrix maths
> rowMeans(M)
[1] 1.5 3.5 5.5
> colMeans(M)
[1] 3.333333 3.666667
> rowSums(M)
[1] 3 7 11
> colSums(M)
[1] 10 11
> t <- t(M);t
[,1] [,2] [,3]
[1,] 2 3 5
[2,] 1 4 6
> inverse <- solve(diag(c(1,2,3))); inverse
[,1] [,2] [,3]
[1,] 1 0.0 0.0000000
[2,] 0 0.5 0.0000000
[3,] 0 0.0 0.3333333
> e <- eigen(diag(c(1,2,3))); e
$values
[1] 3 2 1
$vectors
[,1] [,2] [,3]
[1,] 0 0 1
[2,] 0 1 0
[3,] 1 0 0
> d <- det(diag(c(1,2,3))); d
[1] 6
Matrix indexing [row, col] [[row, col]]
# [[ for single cell selection; [ for multi cell selection
# indexed by positive numbers: these ones
# indexed by negative numbers: not these
# indexed by logical atomic vector: in/out
# named rows/cols can be indexed by name
# M[i] or M[[i]] is vector-like indexing
# $ operator is invalid for atomic vectors
# M[r,]
# M[,c]
Arrays
A three dimensional array created in two steps:
> A <- 1:8;A
[1] 1 2 3 4 5 6 7 8
> dim(A) <- c(2,2,2);A
, , 1
[,1] [,2]
[1,] 1 3
[2,] 2 4
, , 2
[,1] [,2]
[1,] 5 7
[2,] 6 8
A matrix is a special case of array. Matrices are arrays with two dimensions.
> M <- array(1:9, dim=c(3,3));M
[,1] [,2] [,3]
[1,] 1 4 7
[2,] 2 5 8
[3,] 3 6 9
No comments:
Post a Comment