基于名称选择变量(简单正则表达式) [英] Choose variables based on name (simple regular expression)

查看:245
本文介绍了基于名称选择变量(简单正则表达式)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入变量名,这意味着我应该怎么做。我想象一个数据框调查。

 库(Rlab)#需要rbern()函数。 
survey< - data.frame(cbind(
id = seq(1:10))
likert_this = sample(seq(1:7),10,replace = T),
likert_that = sample(seq(1:7),10,replace = T),
dim_bern_varx = rbern(10,0.6),
disc_1 = sample(letters [1:5],10 ,replace = T)))

现在我想对包含 likert ,其他包含 bern 等变量的东西



如何在R中完成? >

解决方案

您可以使用 grep() colnames )

 调查[,grep(bern,colnames(survey))] 


I would like to incorporate variable names that imply what I should do with them. I imagine a dataframe "survey".

library(Rlab) # Needed for rbern() function.
survey <- data.frame(cbind(  
id = seq(1:10),  
likert_this = sample(seq(1:7),10, replace=T),  
likert_that = sample(seq(1:7), 10, replace=T),  
dim_bern_varx = rbern(10, 0.6),  
disc_1 = sample(letters[1:5],10,replace=T)))

Now I would like to do certain things with all variables that contain likert, other things with variables that contain bern etc.

How can this be done in R?

解决方案

You can use grep() with colnames():

survey[,grep("bern", colnames(survey))]

这篇关于基于名称选择变量(简单正则表达式)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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