R - 基于列名的子集 [英] R - Subset based on column name

查看:19
本文介绍了R - 基于列名的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据框有超过 120 列(变量),我想根据列名创建子集.

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

例如,我想创建一个子集,其中列名包含字符串mood".这可能吗?

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

推荐答案

我一般用

    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 - 基于列名的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆