R语言中,cbind和rbind区别

cbind: 根据列进行合并,即叠加所有列,m列的矩阵与n列的矩阵cbind()最后变成m+n列,合并前提:cbind(a, c)中矩阵a、c的行数必需相符

rbind: 根据行进行合并,就是行的叠加,m行的矩阵与n行的矩阵rbind()最后变成m+n行,合并前提:rbind(a, c)中矩阵a、c的列数必需相符
————————————————

R version 3.6.2 (2019-12-12) -- "Dark and Stormy Night"
Copyright (C) 2019 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> a <- matrix(1:12, 3, 4)
> print(a)
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
> b <- matrix(-1:-12, 3, 4)
> print(b)
     [,1] [,2] [,3] [,4]
[1,]   -1   -4   -7  -10
[2,]   -2   -5   -8  -11
[3,]   -3   -6   -9  -12
> x=cbind(a,b)
> print(x)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    4    7   10   -1   -4   -7  -10
[2,]    2    5    8   11   -2   -5   -8  -11
[3,]    3    6    9   12   -3   -6   -9  -12
> y=rbind(a,b)
> y
     [,1] [,2] [,3] [,4]
[1,]    1    4    7   10
[2,]    2    5    8   11
[3,]    3    6    9   12
[4,]   -1   -4   -7  -10
[5,]   -2   -5   -8  -11
[6,]   -3   -6   -9  -12
> c <- matrix(-1:-20, 4, 5)
> c
     [,1] [,2] [,3] [,4] [,5]
[1,]   -1   -5   -9  -13  -17
[2,]   -2   -6  -10  -14  -18
[3,]   -3   -7  -11  -15  -19
[4,]   -4   -8  -12  -16  -20
> x2=cbind(a,c)
Error in cbind(a, c) : number of rows of matrices must match (see arg 2)
> print(x2)
Error in print(x2) : object 'x2' not found
> y2=rbind(a,c)
Error in rbind(a, c) : 
  number of columns of matrices must match (see arg 2)
> print(y2)
Error in print(y2) : object 'y2' not found
> 

版权声明:本文为CSDN博主「SThranduil」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/SThranduil/java/article/details/70316276

你可能感兴趣的:(R语言中,cbind和rbind区别)