R data frame extracting non-sequential columns and creating new sub data frames

霸气de小男生 提交于 2019-12-11 20:24:55

问题


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.frames 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!