0
x <- c('aa', 'ab', 'ac', 'bb', 'bc')

grep('a', x, value=TRUE) 
[1] "aa" "ab" "ac"

However, When I applied to my example, it is not working properly.

 > logit_coef$label
"(Intercept)"     "changer"  "changem"  "revenue"     
 "mou"     "overage"  "roam"   "conference"  "months" "uniqsubs" "custcare"
 "retcalls"  "dropvce"   "eqpdays"   "refurb|yes"  "smartphone|yes"  
 "highcreditr|yes"  "occupation|student"      "occupation|retired"


grep('|', logit_coef$label, value=TRUE)

It gives me all of the columns, I only want to name of columns contain '|'

 "smartphone|yes" "highcreditr|yes"  "occupation|student"      
 "occupation|retired"  That is the result I want
Tom
  • 23
  • 1
  • 11
  • 1
    Can you provide a small reproducible data set using `dput`. You can do `dput(taxi)` assuming the taxi data.frame is small. – steveb Dec 12 '18 at 01:05
  • And if the taxi data frame isn't small enough, do `dput(taxi[1:20, ])` to give us just the first 20 rows. – Gregor Thomas Dec 12 '18 at 02:19
  • I did. I put the data on the post. – Tom Dec 12 '18 at 07:28
  • Add `fixed = TRUE` – markus Mar 03 '19 at 19:16
  • `|` is a special character in regex. You can either use the `fixed = TRUE` argument to skip fancy regex patterns and just do exact matching `grep('|', logit_coef$label, fixed = TRUE)`, or you can escape it with two backslashes, `grep('\\|', logit_coef$label, value = TRUE)` – Gregor Thomas Mar 03 '19 at 19:42

0 Answers0