R Matrix


R matrix is a two dimensional array. R has a lot of operator and functions that make matrix handling very convenient.

Matrix assignment:

> A <- matrix(c(3,5,7,1,9,4),nrow=3,ncol=2,
dimnames=list(c("a","b","c"),c("x","y")))
> A

x y
a 3 1
b 5 9
c 7 4


Matrix row and column count:

> rA <- nrow(A)
> rA

[1] 3

> cA <- ncol(A)
> cA

[1] 2

Convert matrix into a vector:

> as.vector(A)
[1] 3 7 9 5 1 4
> as.vector(t(A))
[1] 3 5 7 1 9 4

Get one or multiple rows and columns of a matrix:

> nrow(esoph) //builtin Smoking Alcohol and Cancer dataset
[1] 88
> v <- esoph[1:5,3:4]
> v
     tobgp ncases
1 0-9g/day      0
2    10-19      0
3    20-29      0
4      30+      0
5 0-9g/day      0
> v <- esoph[1:5,]
> v
  agegp     alcgp    tobgp ncases ncontrols
1 25-34 0-39g/day 0-9g/day      0        40
2 25-34 0-39g/day    10-19      0        10
3 25-34 0-39g/day    20-29      0         6
4 25-34 0-39g/day      30+      0         5
5 25-34     40-79 0-9g/day      0        27

Delete rows or columns in matrix:

> v2 <- v[,-2]
> v2
  agegp    tobgp ncases ncontrols
1 25-34 0-9g/day      0        40
2 25-34    10-19      0        10
3 25-34    20-29      0         6
4 25-34      30+      0         5
5 25-34 0-9g/day      0        27
> v3 <- v[,-c(2,3)]
> v3
  agegp ncases ncontrols
1 25-34      0        40
2 25-34      0        10
3 25-34      0         6
4 25-34      0         5
5 25-34      0        27
> v4 <- v[-c(4,5),]
> v4
  agegp     alcgp    tobgp ncases ncontrols
1 25-34 0-39g/day 0-9g/day      0        40
2 25-34 0-39g/day    10-19      0        10
3 25-34 0-39g/day    20-29      0         6


You may also select the rows or columns that meet your condition.

> subset(esoph,esoph[,5]>40)
   agegp     alcgp    tobgp ncases ncontrols
16 35-44 0-39g/day 0-9g/day      0        60
31 45-54 0-39g/day 0-9g/day      1        46
47 55-64 0-39g/day 0-9g/day      2        49
63 65-74 0-39g/day 0-9g/day      5        48


t(A) function returns a transposed matrix of A:

> B <- t(A)
> B

a b c
x 3 5 7
y 1 9 4


Matrix multplication:

> C <- A * A
> C

x  y
a  9  1
b 25 81
c 49 16


Matrix Addition:

> C <- A + A
> C

x  y
a  6  2
b 10 18
c 14  8

You may divide or minus two matrices if they have the same number of rows and columns.

> B <- matrix(c(8,3,64,13,7,21),nrow=3,ncol=2,byrow=TRUE)
> B
     [,1] [,2]
[1,]    8    3
[2,]   64   13
[3,]    7   21
> B/A
          x        y
a  2.666667 3.000000
b 12.800000 1.444444
c  1.000000 5.250000
> A - B
    x   y
a  -5  -2
b -59  -4
c   0 -17

You may divide a matrix by a vector or vice versa if the matrix's column number equal to the vector's length.

> C <- c(3,4)
> A/C
         x    y
a 1.000000 0.25
b 1.250000 3.00
c 2.333333 1.00
> C%/%A
  x y
a 1 4
b 0 0
c 0 1


Inverse matrix:

> m <- matrix(c(3,5,7,1),nrow=2,ncol=2,byrow=TRUE)
> m
     [,1] [,2]
[1,]    3    5
[2,]    7    1
> solve(m) * m
         [,1]     [,2]
[1,] -0.09375  0.78125
[2,]  1.53125 -0.09375

tr() function of package "psych" get the trace of a matrix.

> install.packages("psych", repo="http://cran.r-project.org",dep=TRUE)
> library("psych")
Warning message:
package 'psych' was built under R version 3.6.3
> x <- matrix(11:19, ncol=3, nrow=3)
> x
	[,1]	[,2]	[,3]
[1,]	11	14	17
[2,]	12	15	18
[3,]	13	16	19
> tr(x)
45

Sometimes a matrix need to be sorted by a specific column, which can be done by using order() function. Following is a csv file example.


Following R code will read in the above file into a matrix, and sort it by column 4, then write to a file.
> x <- read.csv("sortmatrix.csv",header=T,sep=",");
> x
   t1 t2 t3 t4 t5 t6 t7 t8
r1  1  0  1  0  0  1  0  2
r2  1  2  5  1  2  1  2  1
r3  0  0  9  2  1  1  0  1
r4  0  0  2  1  2  0  0  0
r5  0  2 15  1  1  0  0  0
r6  2  2  3  1  1  1  0  0
r7  2  2  3  1  1  1  0  1
> x <- x[order(x[,4]),];
> x
   t1 t2 t3 t4 t5 t6 t7 t8
r1  1  0  1  0  0  1  0  2
r2  1  2  5  1  2  1  2  1
r4  0  0  2  1  2  0  0  0
r5  0  2 15  1  1  0  0  0
r6  2  2  3  1  1  1  0  0
r7  2  2  3  1  1  1  0  1
r3  0  0  9  2  1  1  0  1
> x <- write.table(x,file="tp.txt",sep=",")


endmemo.com © 2024  | Terms of Use | Privacy | Home