问题
I have a data frame consisting of 25 columns and 30,000 rows. I want to create sub-data frames that contain all rows but only certain columns that are in non-sequential order. As an example, a sub data frame containing columns 1, 12, 15, and 25.
I have been playing around with lapply but I dont know how to create the sub data frames with columns that are in non-sequential order.
Thanks.
回答1:
Information from data.frame
s can be extracted by using the indices of a data.frame
's rows, columns, or both.
The general form is of:
dataset[rows_wanted, cols_wanted]
Omitting one of those returns all of what was omitted. For example, dataset[c(1, 2, 3), ]
would return rows one through three, and all the columns.
Thus, for your problem, you can simply do:
dataset[, c(1, 2, 12, 25)]
来源:https://stackoverflow.com/questions/19757554/r-data-frame-extracting-non-sequential-columns-and-creating-new-sub-data-frames