全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 R语言论坛
894 2
2016-04-15
在书上看到如果带有行名的矩阵添加到一个矩阵中去,可以根据行名进行调整。而将矢量添加到矩阵中去,就不会调整顺序。我试了一下,怎么也不行。求大神指教。
> test<-matrix(c(seq(1,24,1)),ncol=6)
> test
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    5    9   13   17   21
[2,]    2    6   10   14   18   22
[3,]    3    7   11   15   19   23
[4,]    4    8   12   16   20   24

> another<-c(28,27,26,25)
> cbind(test,another)
                                 another
[1,] 1 5  9 13 17 21      28
[2,] 2 6 10 14 18 22      27
[3,] 3 7 11 15 19 23      26
[4,] 4 8 12 16 20 24      25

> rname<-c('a','b','c','d')
> cname<-c('U','V','W','X','Y','Z')
> rownames(test)<-rname
> colnames(test)<-cname

> test
  U V  W  X  Y  Z
a 1 5  9 13 17 21
b 2 6 10 14 18 22
c 3 7 11 15 19 23
d 4 8 12 16 20 24

> another<-matrix(c(28,27,26,25),ncol=1)
> another
     [,1]
[1,]   28
[2,]   27
[3,]   26
[4,]   25
> rownames(another)<-c('d','c','b','a')
> another
  [,1]
d   28
c   27
b   26
a   25

> cbind(test,another)
  U V  W  X  Y  Z   
a 1 5  9 13 17 21 28
b 2 6 10 14 18 22 27
c 3 7 11 15 19 23 26
d 4 8 12 16 20 24 25



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2016-4-15 20:11:31
R不会自动帮你按照行名进行调整。自己动手吧。

复制代码
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2016-4-15 22:32:13
我不知道你是在哪里看到的可以根据行名自动对行。根据帮助里的说明:For cbind row names are taken from the first argument with appropriate names: rownames for a matrix, or names for a vector of length the number of rows of the result.

cbind只从第一个变量提取行名作为结果的行名,但所有的合并依然和之前的一样进行,无关第二个变量是否有行名。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群