使用非连续行号的子集数据 [英] Subset data using non-sequential row numbers

查看:47
本文介绍了使用非连续行号的子集数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含30行和100列(X)的数据框。

I have a data frame with 30 rows and 100 columns (X).

我想创建一个新的数据框(Y),其中包含特定行较大的数据框。

I would like to create a new data frame (Y) with specific rows from the larger data frame.

例如,我希望数据框(Y)包含行1至5、10至14和20。

For example, I would like data frame (Y) to contain rows 1 through 5, 10 through 14, and 20.

我知道我可以使用以下代码:

I know that I can use the code:

Y<-X[1:5,]

并获取前五行,但我无法得出类似的代码来获取行1 :5、10:14和20。

and obtain the first five rows, but I cannot work out a similar code to obtain rows 1:5, 10:14, and 20.

推荐答案

通常,在数据框或矩阵中选择行时,使用熟悉的X [rows,cols]格式。请记住,这两个参数不仅可以生成为简单的数字或序列,还可以通过数字和序列的串联生成。因此,对于您的问题,您可以使用类似以下的内容:

Generally, when selecting rows in a data frame or matrix, one uses the familiar X[rows, cols] format. It's helpful to remember that both of the parameters can be generated not simply as simple numbers or sequences, but also through the concatenation of numbers and sequences. Therefore, for your problem you can use something like the following:

Y <- X[c(1:5, 10:14, 20), ]

这将选择第1至5行,第10至14行以及第20行以及X中的所有列,然后将结果分配给Y。

This will select rows 1 through 5, rows 10 through 14, and row 20, together with all of the columns in X, and assign the result to Y.

这篇关于使用非连续行号的子集数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆