R - Subset based on column name

2019-05-10 03:00发布

问题:

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.



标签: r subset