0% found this document useful (0 votes)
70 views6 pages

Ho - R Matrix Commands

This document summarizes various matrix commands in R, including how to create, manipulate, and analyze matrices. Key points include: 1) The matrix() function is used to create matrices and requires specifying the data, number of rows, and number of columns. Common matrix operations like addition, subtraction, and multiplication are demonstrated. 2) Functions like is.matrix(), nrow(), ncol(), rowSums(), colSums(), etc. can be used to analyze properties of matrices such as dimensions, ranks, sums, and means. 3) Operations for joining matrices together horizontally (cbind()) and vertically (rbind()) are presented. Transposes, inverses, and decompositions like QR are also

Uploaded by

techwizseetha
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)
70 views6 pages

Ho - R Matrix Commands

This document summarizes various matrix commands in R, including how to create, manipulate, and analyze matrices. Key points include: 1) The matrix() function is used to create matrices and requires specifying the data, number of rows, and number of columns. Common matrix operations like addition, subtraction, and multiplication are demonstrated. 2) Functions like is.matrix(), nrow(), ncol(), rowSums(), colSums(), etc. can be used to analyze properties of matrices such as dimensions, ranks, sums, and means. 3) Operations for joining matrices together horizontally (cbind()) and vertically (rbind()) are presented. Transposes, inverses, and decompositions like QR are also

Uploaded by

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

Newsom

Psy 523/623 Structural Equation Modeling, Spring 2018 1

Phil Ender's Summary of Matrix Commands in R


http://www.philender.com/courses/multivariate/notes/matr.html

The Matrix

# the matrix function


# R wants the data to be entered by columns starting with column one
# 1st arg: c(2,3,-2,1,2,2) the values of the elements filling the columns
# 2nd arg: 3 the number of rows
# 3rd arg: 2 the number of columns

> A <- matrix(c(2,3,-2,1,2,2),3,2)


> A

[,1] [,2]
[1,] 2 1
[2,] 3 2
[3,] -2 2

Is Something a Matrix

> is.matrix(A)

[1] TRUE

> is.vector(A)

[1] FALSE

Multiplication by a Scalar

> c <- 3
> c*A

[,1] [,2]
[1,] 6 3
[2,] 9 6
[3,] -6 6

Matrix Addition & Subtraction

> B <- matrix(c(1,4,-2,1,2,1),3,2)


> B

[,1] [,2]
[1,] 1 1
[2,] 4 2
[3,] -2 1

> C <- A + B
> C

[,1] [,2]
[1,] 3 2
[2,] 7 4
[3,] -4 3

> D <- A - B
> D

[,1] [,2]
[1,] 1 0
[2,] -1 0
[3,] 0 1

Matrix Multiplication

> D <- matrix(c(2,-2,1,2,3,1),2,3)


Newsom
Psy 523/623 Structural Equation Modeling, Spring 2018 2

> D

[,1] [,2] [,3]


[1,] 2 1 3
[2,] -2 2 1

> C <- D %*% A


> C

[,1] [,2]
[1,] 1 10
[2,] 0 4

> C <- A %*% D


> C

[,1] [,2] [,3]


[1,] 2 4 7
[2,] 2 7 11
[3,] -8 2 -4

> D <- matrix(c(2,1,3),1,3)


> D

[,1] [,2] [,3]


[1,] 2 1 3

> C <- D %*% A


> C

[,1] [,2]
[1,] 1 10

> C <- A %*% D

Error in A %*% D : non-conformable arguments

Transpose of a Matrix

> AT <- t(A)


> AT

[,1] [,2] [,3]


[1,] 2 3 -2
[2,] 1 2 2

> ATT <- t(AT)


>ATT

[,1] [,2]
[1,] 2 1
[2,] 3 2
[3,] -2 2

Common Vectors

Unit Vector

> U <- matrix(1,3,1)


> U

[,1]
[1,] 1
[2,] 1
[3,] 1

Zero Vector

> Z <- matrix(0,3,1)


> Z
Newsom
Psy 523/623 Structural Equation Modeling, Spring 2018 3

[,1]
[1,] 0
[2,] 0
[3,] 0

Common Matrices

Unit Matrix

> U <- matrix(1,3,2)


> U

[,1] [,2]
[1,] 1 1
[2,] 1 1
[3,] 1 1

Zero Matrix

> Z <- matrix(0,3,2)


> Z

[,1] [,2]
[1,] 0 0
[2,] 0 0
[3,] 0 0

Diagonal Matrix

> S <- matrix(c(2,3,-2,1,2,2,4,2,3),3,3)


> S

[,1] [,2] [,3]


[1,] 2 1 4
[2,] 3 2 2
[3,] -2 2 3

> D <- diag(S)


> D

[1] 2 2 3

> D <- diag(diag(S))


> D

[,1] [,2] [,3]


[1,] 2 0 0
[2,] 0 2 0
[3,] 0 0 3

Identity Matrix

> I <- diag(c(1,1,1))


> I

[,1] [,2] [,3]


[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

Symmetric Matrix

> C <- matrix(c(2,1,5,1,3,4,5,4,-2),3,3)


> C

[,1] [,2] [,3]


[1,] 2 1 5
Newsom
Psy 523/623 Structural Equation Modeling, Spring 2018 4

[2,] 1 3 4
[3,] 5 4 -2

> CT <- t(C)


> CT

[,1] [,2] [,3]


[1,] 2 1 5
[2,] 1 3 4
[3,] 5 4 -2

Inverse of a Matrix

> A <- matrix(c(4,4,-2,2,6,2,2,8,4),3,3)


> A

[,1] [,2] [,3]


[1,] 4 2 2
[2,] 4 6 8
[3,] -2 2 4

> AI <- solve(A)


> AI

[,1] [,2] [,3]


[1,] 1.0 -0.5 0.5
[2,] -4.0 2.5 -3.0
[3,] 2.5 -1.5 2.0

> A %*% AI

[,1] [,2] [,3]


[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

> AI %*% A

[,1] [,2] [,3]


[1,] 1 0 0
[2,] 0 1 0
[3,] 0 0 1

Inverse & Determinant of a Matrix

> C <- matrix(c(2,1,6,1,3,4,6,4,-2),3,3)


> C

[,1] [,2] [,3]


[1,] 2 1 6
[2,] 1 3 4
[3,] 6 4 -2

> CI <- solve(C)


CI

[,1] [,2] [,3]


[1,] 0.2156863 -0.25490196 0.13725490
[2,] -0.2549020 0.39215686 0.01960784
[3,] 0.1372549 0.01960784 -0.04901961

> d <- det(C)


> d

[1] -102

Rank of a MatrixM/h4>

> A <- matrix(c(2,3,-2,1,2,2,4,7,0),3,3)


> A
Newsom
Psy 523/623 Structural Equation Modeling, Spring 2018 5

[,1] [,2] [,3]


[1,] 2 1 4
[2,] 3 2 7
[3,] -2 2 0

> matA <- qr(A)


> matA$rank

[1] 3

> A <- matrix(c(2,3,-2,1,2,2,4,6,-4),3,3)


> A

[,1] [,2] [,3]


[1,] 2 1 4
[2,] 3 2 6
[3,] -2 2 -4

> matA <- qr(A)


> matA$rank

[1] 2

# note column 3 is 2 times column 1

Number of Rows & Columns

> X <- matrix(c(3,2,4,3,2,-2,6,1),4,2)


> X

[,1] [,2]
[1,] 3 2
[2,] 2 -2
[3,] 4 6
[4,] 3 1

> dim(X)

[1] 4 2

> r <- nrow(X)


> r

[1] 4

> c <- ncol(X)


> c

[1] 2

Computing Column & Row Sums

# note the uppercase S

> A <- matrix(c(2,3,-2,1,2,2),3,2)


> A

[,1] [,2]
[1,] 2 1
[2,] 3 2
[3,] -2 2

> c <- colSums(A)


> c

[1] 3 5

> r <- rowSums(A)


> r

[1] 3 5 0
Newsom
Psy 523/623 Structural Equation Modeling, Spring 2018 6

> a <- sum(A)


> a

[1] 8

Computing Column & Row Means

# note the uppercase M

> cm <- colMeans(A)


> cm

[1] 1.000000 1.666667

> rm <- rowMeans(A)


> rm

[1] 1.5 2.5 0.0

> m <- mean(A)


> m

[1] 1.333333

Horizontal Concatenation

> A

[,1] [,2]
[1,] 2 1
[2,] 3 2
[3,] -2 2

> B <- matrix(c(1,3,2,1,4,2),3,2)


> B

[,1] [,2]
[1,] 1 1
[2,] 3 4
[3,] 2 2

> C <- cbind(A,B)


> C

[,1] [,2] [,3] [,4]


[1,] 2 1 1 1
[2,] 3 2 3 4
[3,] -2 2 2 2

Vertical Concatenation (Appending)

> C <- rbind(A,B)


> C

[,1] [,2]
[1,] 2 1
[2,] 3 2
[3,] -2 2
[4,] 1 1
[5,] 3 4
[6,] 2 2

Multivariate Course Web Page

Phil Ender, 13jul07, 23feb05

You might also like