简体   繁体   English

R是向量1行矩阵或1列矩阵

[英]R is a vector a 1 row matrix or 1 column matrix

a <- c(1,2,3)
b <- c(2,4,6)

Then: 然后:

c = cbind(a,b)

In this case, I expect the result is a 1x6 matrix, but it turns out a 3x 2 matrix 在这种情况下,我希望结果是一个1x6矩阵,但结果却是3x 2矩阵

d = rbind(a,b)

This turns out a 2x3 matrix. 结果是一个2x3矩阵。 Why are the structures of a and b not consistent? 为什么a和b的结构不一致? What are the underlined rules here? 这里有下划线的规则是什么?

a and b are plain vectors (not arrays) and cbind treats plain vectors (and one dimensional arrays) as columns while rbind treats plain vectors (and one dimensional arrays) as rows. ab是普通矢量(不是数组), cbind将普通矢量(和一维数组)视为列,而rbind将普通矢量(和一维数组)视为行。

If given 2 dimensional inputs then they do act in the way described in the question. 如果给出二维输入,则它们确实会以问题中描述的方式起作用。

For example, 例如,

A13 <- matrix(1:3, 1)  # 1x3 matrix
B13 <- matrix(4:6, 1)  # 1x3 matrix

cbind(A13, B13)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    1    2    3    4    5    6

rbind(A13, B13)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6

A31 <- matrix(1:3, 3)  # 3x1 matrix
B31 <- matrix(4:6, 3)  # 3x1 matrix

rbind(A31, B31)
##      [,1]
## [1,]    1
## [2,]    2
## [3,]    3
## [4,]    4
## [5,]    5
## [6,]    6

cbind(A31, B31)
##      [,1] [,2]
## [1,]    1    4
## [2,]    2    5
## [3,]    3    6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM