使用行名称的子集数据框 [英] Subset data frame using row names
本文介绍了使用行名称的子集数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的数据框:
I have a data frame like this:
x1 x2 x3
271 3 5 2
553 2 4 1
563 2 5 3
当我尝试根据行名称选择一行时,例如271,报错:
When I try to select a row based on the row name, e.g. 271, it errors:
df[271, ]
# Error in xj[i, , drop = FALSE] : subscript out of bounds
如何根据行名称(例如 271、553、563)选择这些行?
How can I select these rows based on the row names (e.g. 271, 553, 563)?
推荐答案
您需要引用 data.frame 的行名:
You need to reference the rownames of your data.frame:
dfsub[rownames(dfsub) == 271,] #where dfsub is your subsetted data.frame
正如@koekenbakker 所评论的,有一个使用''
来引用行名的简写.所以这将是:
as @koekenbakker commented, there is a shorthand to reference the rownames by using ''
. So this would be:
dfsub['271',] #where dfsub is your subsetted data.frame and 271 the rowname
这篇关于使用行名称的子集数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文