用行名打印一列数据框 [英] Print one column of data frame with row names
本文介绍了用行名打印一列数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
>数据< - data.frame(a = 1:3,b = 2:4,c = 3:5,row.names = c(x,y,z))
> ;数据
abc
x 1 2 3
y 2 3 4
z 3 4 5
如果我选择多个列,则R将打印它们与行名称:
>数据[,c(a,c)]
ac
x 1 3
y 2 4
z 3 5
但是,如果我只选择只有一列,R将其打印为一个简单的向量,不带行名称:
>数据[,c]
[1] 3 4 5
我的问题是,如何告诉R打印一列,方法与打印多列相同,即列名称?
解决方案
您可以使用 drop
参数(另见?'['
):
data [,c,drop = FALSE]
这是否有帮助?
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:
> 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:
> 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?
解决方案
You can use the drop
argument (see also ?'['
):
data[,"c", drop=FALSE]
Does this help?
这篇关于用行名打印一列数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文