简体   繁体   English

r - grepl vs比赛

[英]r - grepl vs matches

I am experiencing a little problem using the matches function from dplyr package. 我在使用dplyr包中的matches函数时遇到了一些问题。

From this dataset, I would like to extract the column names starting with enj 从这个数据集中,我想提取以enj开头的列名

    enj1 enj2 Enjm
bbc    1    1    2
bca    1    1    2

With grepl , I can do this 有了grepl ,我可以做到这一点

dt[, grepl('enj', colnames(dt))] 

and get 得到

    enj1 enj2
bbc    1    1
bca    1    1

However the function matches does not give me the correct answer 但是函数matches没有给我正确的答案

library(dplyr) 

dt %>% select(matches('enj') )
# or 
dt %>% select(matches('^enj') )

Any idea why ? 知道为什么吗?

dt = structure(list(enj1 = structure(c(1L, 1L), .Names = c("bbc", 
"bca"), .Label = "1", class = "factor"), enj2 = structure(c(1L, 
1L), .Names = c("bbc", "bca"), .Label = "1", class = "factor"), 
    Enjm = structure(c(1L, 1L), .Names = c("bbc", "bca"), .Label = "2", class = "factor")), .Names = c("enj1", 
"enj2", "Enjm"), row.names = c("bbc", "bca"), class = "data.frame")

It's because you didn't set ignore.case = F . 这是因为你没有设置ignore.case = F

> dt %>% select(matches('^enj',  ignore.case = F) )
    enj1 enj2
bbc    1    1
bca    1    1
> 

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

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