Subset data using non-sequential row numbers

前端 未结 1 848
悲哀的现实
悲哀的现实 2021-01-02 15:43

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

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

For example,

相关标签:
1条回答
  • 2021-01-02 16:40

    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), ]
    

    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.

    0 讨论(0)
提交回复
热议问题