简体   繁体   English

R - 基于列名的子集

[英]R - Subset based on column name

My data frame has over 120 columns (variables) and I would like to create subsets bases on column names. 我的数据框有超过120列(变量),我想基于列名创建子集。

For example I would like to create a subset where the column name includes the string "mood". 例如,我想创建一个子列,其中列名包含字符串“mood”。 Is this possible? 这可能吗?

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. 我非常清楚,“,”不是必需的,并且名称可以用名称替换,但它不适用于矩阵,我讨厌在更改对象时改变形式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM