选择包含特定字符串/ ID的列

时间:2017-04-24 11:56:48

标签: r select dplyr tidyr

我有一个大约有300 columns的大型数据框。 在列名称的开头,有一个代码。

例如,列是:

GSUP_02X Which supplier do you use for each of the following services? Telephone line rental

或其他列名是:

GSUP_03X Which supplier do you use for each of the following services? Fixed broadband

GSUP_02X and GSUP_03X are codes. 

所以我想选择与vector中的代码列表匹配的所有列。

我试过了:

columns <- c("GSUP_02X","GSUP_03X")
consumer_brand_nps %>%
                      select(contains(columns))

但是得到以下错误:

Error: is.string(match) is not TRUE

任何其他tidyrdplyr解决方案?

2 个答案:

答案 0 :(得分:2)

我们可以matches paste一起使用library(dplyr) consumer_brand_nps %>% select(matches(paste(columns, collapse="|"))) # GSUP_02X GSUP_03X #1 -0.545880758 -1.3169081 #2 0.536585304 0.5982691 #3 0.419623149 -0.7622144 #4 -0.583627199 -1.4290903 #5 0.847460017 0.3322444 #6 0.266021979 -0.4690607 #7 0.444585270 -0.3349868 #8 -0.466495124 1.5362522 #9 -0.848370044 0.6099945 #10 0.002311942 0.5163357

set.seed(24)
consumer_brand_nps <- as.data.frame(matrix(rnorm(10*5), ncol=5, nrow=10,
              dimnames = list(NULL, c(columns, LETTERS[1:3]))))

数据

mysql_connect('localhost', 'root', ''); 
mysql_select_db('cinema'); 
$email = filter_input(INPUT_POST, 'email');
$pword = filter_input(INPUT_POST, 'pword');
$query = "INSERT INTO customers (email, password) VALUES ('$email', '$pword')";
mysql_query($query);`

答案 1 :(得分:0)

使用来自@akrun的数据进行测试。

consumer_brand_nps %>%
  select_(.dots = columns ) ## note the underscore