R - Subset based on column name

与世无争的帅哥 提交于 2019-12-07 13:10:24

问题


My data frame has over 120 columns (variables) and I would like to create subsets bases on column names.

For example I would like to create a subset where the column name includes the string "mood". Is this possible?


回答1:


I generally use

    SubData <- myData[,grep("whatIWant", colnames(myData))]

I know very well that the "," is not necessary and colnames could be replaced by names but it would not work with matrices and I hate to change the formalism when changing objects.



来源:https://stackoverflow.com/questions/28815508/r-subset-based-on-column-name

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