[英]How to return 2 specific rows from a dataframe?
firstVector <- c("A", "B", "C", "D", "E")
secondVector <- c(1, 2, 3, 4, 5)
thirdVector <- c("a", "b", "c", "d", "e")
myDataFrame <- data.frame(firstVector, secondVector, thirdVector)
How do I extract row 3 and 4 from my data frame?如何从数据框中提取第 3 行和第 4 行? I want to print it row 3 and 4 in order it to look like this:
我想将它打印在第 3 行和第 4 行,使其看起来像这样:
firstVector secondVector thirdVector
3 C 3 c
4 D 4 d
You can subset your dataframe like this [rows,columns]:您可以像这样 [rows,columns] 子集您的数据框:
myDataFrame[c(3,4),]
In your case you want a vector containing rows 3 and 4, therefore c(3,4), you can add more columns in the vector to subset more rows, for example c(1,2,3,12).在您的情况下,您需要一个包含第 3 行和第 4 行的向量,因此 c(3,4),您可以在向量中添加更多列以对更多行进行子集化,例如 c(1,2,3,12)。 If you dont provide an argument it returns the whole dimension.
如果您不提供参数,它将返回整个维度。 In your example you subset rows, and return all the columns
在您的示例中,您对行进行子集化,并返回所有列
it's the same for columns:列也是一样的:
myDataFrame[c(3,4),c(1,2)]
you can subset rows 3 and 4 and columns 1 and 2.您可以对第 3 行和第 4 行以及第 1 行和第 2 列进行子集化。
Another way to do this is using : c(1:4) means from 1 to 4另一种方法是使用:c(1:4) 表示从 1 到 4
Hope this helps希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.