使用行名称打印一列数据框

时间:2022-09-04 21:45:04

Consider a data frame with custom row names:

考虑具有自定义行名称的数据框:

> data <- data.frame(a=1:3,b=2:4,c=3:5,row.names=c("x","y","z"))
> data
  a b c
x 1 2 3
y 2 3 4
z 3 4 5

If I select more than one column, R prints them along with the row names:

如果我选择多个列,R会将它们与行名称一起打印:

> data[,c("a","c")]
  a c
x 1 3
y 2 4
z 3 5

But if I select only one column, R prints it as a simple vector, without the row names:

但是如果我只选择一列,则R将其打印为一个简单的向量,而不是行名:

> data[,"c"]
[1] 3 4 5

My question is, how do I tell R to print one column in the same way it prints multiple columns, that is, with the row names?

我的问题是,如何告诉R以打印多列的相同方式打印一列,即使用行名称?

1 个解决方案

#1


33  

You can use the drop argument (see also ?'['):

你可以使用drop参数(参见?'['):

data[,"c", drop=FALSE]

Does this help?

这有帮助吗?

#1


33  

You can use the drop argument (see also ?'['):

你可以使用drop参数(参见?'['):

data[,"c", drop=FALSE]

Does this help?

这有帮助吗?