如何从数据框中选择具有特定行名的行? [英] How to select some rows with specific rownames from a dataframe?
本文介绍了如何从数据框中选择具有特定行名的行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含几行的数据框。我想从此数据框中选择一些具有特定行名的行(例如 stu2,stu3,stu5,stu9
)。输入示例数据帧如下:
I have a data frame with several rows. I want to select some rows with specific rownames (such as stu2,stu3,stu5,stu9
) from this dataframe. The input example dataframe is as follows:
attr1 attr2 attr3 attr4
stu1 0 0 1 0
stu2 -1 1 -1 1
stu3 1 -1 0 -1
stu4 1 -1 1 -1
stu5 -1 1 0 1
stu6 1 -1 1 0
stu7 -1 -1 -1 1
stu8 1 -1 0 -1
stu9 -1 -1 1 -1
stu10 -1 1 0 1
预期输出:
attr1 attr2 attr3 attr4
stu2 -1 1 -1 1
stu3 1 -1 0 -1
stu5 -1 1 0 1
stu9 -1 -1 1 -1
推荐答案
假设您有一个名为的数据框学生
,则可以使用方括号语法选择单个行或列,例如:
Assuming that you have a data frame called students
, you can select individual rows or columns using the bracket syntax, like this:
-
学生[1,2]
第1行和第2列,结果将是单个单元格。 -
students [1,]
将选择所有行1,学生[,2]
将选择第2列的全部。
students[1,2]
would select row 1 and column 2, the result here would be a single cell.students[1,]
would select all of row 1,students[,2]
would select all of column 2.
如果要选择多个行或列,请使用值列表,如下所示:
If you'd like to select multiple rows or columns, use a list of values, like this:
-
学生[c(1,3,4),]
将选择第1、3和4行, -
students [c( stu1 , stu2),]
会选择名为stu1
和stu2
的行。 / li>
students[c(1,3,4),]
would select rows 1, 3 and 4,students[c("stu1", "stu2"),]
would select rows namedstu1
andstu2
.
希望我能帮上忙。
这篇关于如何从数据框中选择具有特定行名的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文