ForeWord
Hello,everyone. I also again meet you in the article. Maybe the life is not easy, but we still find a way to solve all kinds of problems. The R is excellent data analysis Lanuage in Computer Subject. The data is information, it could help us analysis and predict many things.
matrix
The matrix is one important type in data process. It has more deminations than vector. it could process complex dataset, it could also express more labels to mark the dataset. This's benift for itself.
Example1:
R
> zero = matrix(0,nrow=2,ncol=3)
> print(zero)
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
> dig <- matrix(1,nrow=3,ncol=3)
> print(dig)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
[3,] 1 1 1
> dig = diag(rep(1,4))
> print(dig)
[,1] [,2] [,3] [,4]
[1,] 1 0 0 0
[2,] 0 1 0 0
[3,] 0 0 1 0
[4,] 0 0 0 1
The matrix(x,y,z)function, x is element, y is nrow, z is ncol. The zero is total 0 matrix, dig is total 1 matrix, the diag(rep(1,4)) is diagonal both element matrix. About the dig, it's one diagonal elements both 1, another elements both 0 matrix.
Example2:
R
> dig5 <- diag(rep(4:1))
> print(dig5)
[,1] [,2] [,3] [,4]
[1,] 4 0 0 0
[2,] 0 3 0 0
[3,] 0 0 2 0
[4,] 0 0 0 1
> dig11 <- diag(rep(2:7))
> print(dig11)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 2 0 0 0 0 0
[2,] 0 3 0 0 0 0
[3,] 0 0 4 0 0 0
[4,] 0 0 0 5 0 0
[5,] 0 0 0 0 6 0
[6,] 0 0 0 0 0 7
>
The diag(rep(x:y)) function, x is the start value, y is end value, it could ascending order, it also could descending order.
Example3:
R
> mx <- matrix(3,nrow=2,ncol=4)
> print(mx)
[,1] [,2] [,3] [,4]
[1,] 3 3 3 3
[2,] 3 3 3 3
> dim(mx)
[1] 2 4
> nrow(mx)
[1] 2
> ncol(mx)
[1] 4
>
The dim(matrix) function, parameter is matrix, dim could gain the matrix demination, it's the matrix's nrow and ncol when the matrix is created.
Example4:
R
> mx2 <- matrix(1:8,nrow=2,ncol=4)
> print(mx2)
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> mx2[1:2,3:4]
[,1] [,2]
[1,] 5 7
[2,] 6 8
> mx2[1,2:4]
[1] 3 5 7
> mx2[1:2,1:3]
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
>
The Demo is show how to do scling operation about matrix. The matrix scling could generate new vector or new matrix.
Example5:
R
> colnames(mx2) <- paste0("Col",1:4)
> rownames(mx2) <- paste0("Row",1:2)
> print(mx2)
Col1 Col2 Col3 Col4
Row1 1 3 5 7
Row2 2 4 6 8
> colnames(mx2) <- paste0("C",4:1)
> rownames(mx2) <- paste0("R",2:1)
> print(mx2)
C4 C3 C2 C1
R2 1 3 5 7
R1 2 4 6 8
> ncol(mx2)
[1] 4
> nrow(mx2)
[1] 2
>
The colnames(matrix), the parameter is matrix. paste0(x,y), the function is number 0, it isn't letter o. The x is mark label, y is scling content. The Scling operation is follow the nrow or ncol.
Example6:
> A = matrix(1:10,nrow=2,ncol=5,byrow=T)
> print(A)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 4 5
[2,] 6 7 8 9 10
> A2 = matrix(1:10,nrow=2,ncol=5)
> print(A2)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
>
The A is from 1 to 10 elements, it has 2 nrows, 5 ncols, it follows one rulefill with row, the byrow parameter default F value. When you adjust the value to T, the switch will take effect.
Example7:
> x = matrix(1:15,nrow=3,ncol=5)
> print(x)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 7 10 13
[2,] 2 5 8 11 14
[3,] 3 6 9 12 15
> x2 <- matrix(21,nrow=2,ncol=5)
> rbind(x,x2)
[,1] [,2] [,3] [,4] [,5]
[1,] 1 4 7 10 13
[2,] 2 5 8 11 14
[3,] 3 6 9 12 15
[4,] 21 21 21 21 21
[5,] 21 21 21 21 21
> x3 <- matrix(34,nrow=3,ncol=2)
> print(x3)
[,1] [,2]
[1,] 34 34
[2,] 34 34
[3,] 34 34
> cbind(x,x3)
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
[1,] 1 4 7 10 13 34 34
[2,] 2 5 8 11 14 34 34
[3,] 3 6 9 12 15 34 34
>
From the above codes, we know the rbind(x,y), the function is fill with row that expand the row, it requires the x ncol as same y. we also know the cbind(x,y), the function is fill with col that expand the col, it requires the x nrow as same as y.
Example8:
> M <- matrix(1:8,nrow=2,ncol=4)
> print(M)
[,1] [,2] [,3] [,4]
[1,] 1 3 5 7
[2,] 2 4 6 8
> M2 <- matrix(2:9,nrow=4,ncol=2)
> print(M2)
[,1] [,2]
[1,] 2 6
[2,] 3 7
[3,] 4 8
[4,] 5 9
> M3 = M2 * M
Error in M2 * M : non-conformable arrays
> M3 = M2 %*% M
> print(M3)
[,1] [,2] [,3] [,4]
[1,] 14 30 46 62
[2,] 17 37 57 77
[3,] 20 44 68 92
[4,] 23 51 79 107
> M4 = matrix(3:10,nrow=4,ncol=2)
> M2 * M4
[,1] [,2]
[1,] 6 42
[2,] 12 56
[3,] 20 72
[4,] 30 90
> M4
[,1] [,2]
[1,] 3 7
[2,] 4 8
[3,] 5 9
[4,] 6 10
From the above result, we know the matrix * matrix , it must sure the two matrixs both same deminations. But the matrix %*% matrix, you must sure the matrix row number and another matrix col number is same.
Example9:
> k <- matrix(1,nrow=2,ncol=3)
> k2 <- t(k)
> print(k2)
[,1] [,2]
[1,] 1 1
[2,] 1 1
[3,] 1 1
> print(k)
[,1] [,2] [,3]
[1,] 1 1 1
[2,] 1 1 1
> nrow(k);ncol(k)
[1] 2
[1] 3
> nrow(k2);ncol(k2)
[1] 3
[1] 2
>
From the above codes, we know the t() function, it could achieve matrix transpose.
Example10:
> g <- matrix(c(2,4,6,1,4,8,9,5),nrow=2,ncol=4)
> print(g)
[,1] [,2] [,3] [,4]
[1,] 2 6 4 9
[2,] 4 1 8 5
> g <- matrix(c(3,4,2,1,7,8,0,4),nrow=2,ncol=4,byrow=T)
> print(g)
[,1] [,2] [,3] [,4]
[1,] 3 4 2 1
[2,] 7 8 0 4
>
From the above codes, we could through c(x1,x2,x.....) this way to custom matrix element.
Example11:
> j <- matrix(c(1,4,2,3,6,7,5,9,0),nrow=3,ncol=3)
> print(j)
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 4 6 9
[3,] 2 7 0
> solve(j)
[,1] [,2] [,3]
[1,] -0.8873239 0.49295775 -0.04225352
[2,] 0.2535211 -0.14084507 0.15492958
[3,] 0.2253521 -0.01408451 -0.08450704
>
From the above codes, we know the solve(matrix) function. It could calculate the Inverse of a matrix.
In the End
We will say Goodbye in here now. The R is one important data analysis language. It isn't easy, but I have confidence to you, R also has one unique advantage, do you find it? if you couldn't catch it, just a pity. My dear friends, please you remember the rule: The will could take you jump out the trouble environment. Your PanOne boring and irritable boy in male.