R_day3(DataType:matrix)

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.

相关推荐
喵本喵叁肆2 天前
Rényi 差分隐私:把隐私损失看成一族矩(英文原题:Rényi Differential Privacy (CSF 2017))
开发语言·r语言
电科_银尘7 天前
【深度学习项目实战·17】行人目标检测:用Faster R-CNN找出图片中的行人
深度学习·目标检测·r语言
Biotree_202516 天前
实验谱库检索不再是唯一答案:AIMe依托预测质谱空间,实现小分子MS²全域注释检索
r语言
Canmag 兴隆磁性19 天前
回复磁导率 μ_R:永磁体在动态磁路中的关键参数
开发语言·学习·r语言·永磁材料·充磁·磁导率
临床数据科学和人工智能兴趣组21 天前
提取匹配模式的第一个值
人工智能·r语言·1024程序员节·r语言-4.2.1·临床试验·临床科研·科学问题
临床数据科学和人工智能兴趣组22 天前
在数据分析和处理过程中,字符串的操作常常扮演着极其重要的角色。R语言内置了多种功能强大的字符串处理函数,gsub() 就是其中之一
数据分析·r语言·r语言-4.2.1·临床研究·临床试验·临床科研
dfjdkfahkjd1 个月前
开源项目吐槽大会:技术文章大纲
r语言
爱读书的小胖1 个月前
Codewars:Last digit of a large number(C)解题分析
c语言·开发语言·r语言
生态学者1 个月前
Global Change Biology | 森林越“步调一致”,反而越怕干旱?树木生长与叶片活动的同步之谜
大数据·人工智能·算法·r语言·微信公众平台