R: 更改行名及矩阵数据提取的易错点——下标出界

利用rownames()更改行名,书写代码如下,但运行后报错“下标出界”。

> rownames(contrib_1) <- contrib_1[c("aa","bb","cc","dd","ee"),]
Error in contrib_1[c("aa","bb",  : 
  下标出界

仔细对比发现,正确代码应为代码1,代码2运行则是提取行名为指定值的所有行数据。
将代码1与代码2混合书写导致了上述代码的报错内容,故需格外注意两者的区别。
代码1:

rownames(contrib_1) <- c("aa","bb","cc","dd","ee")

代码2:

contrib_1 <- contrib[c("aa","bb","cc","dd","ee"),]

你可能感兴趣的:(R)