没有grep功能与"("?

时间:2016-11-09 23:47:05

标签: r regex

这是我的数据集

 userId               source           transactions
         (dbl)                      (chr)        (chr)              
1  1       google / cpc, google / cpc            0, 1            
2  2       (direct) / (none)                     0               
3  3       (direct) / (none)                     1               
4  4        google / organic, (direct) / (none)  0                 
5  5        google / organic                     0                  
6  6        google / organic                     0      

我想提取包含(direct) / (none)

的所有行

我写了以下代码:

output<-df[grep("(direct) / (none)", df$source),]

但是它导致0观察的输出,它与google / cpc等其他人一起使用。怎么了?这是&#34;(&#34;?

的问题

这是dput

dput(df)
structure(list(userId = c(1, 2, 3, 
4, 5, 6, 7, 8, 
9, 10), source = c("google / cpc, google / cpc", 
"(direct) / (none)", "(direct) / (none)", "google / organic", 
"google / organic", "google / organic", "(direct) / (none)", 
"google / cpc, google / cpc, google / cpc, google / organic, google / cpc", 
"(direct) / (none)", "(direct) / (none)"), transactions = c("0, 1", 
"0", "1", "0", "0", "0", "0", "0, 0, 0, 0, 0", "0", "1")), .Names = c("userId", 
"source", "transactions"), class = c("tbl_df", "data.frame"
), row.names = c(NA, -10L))

1 个答案:

答案 0 :(得分:3)

(a special meaning in regex。你应该逃避它\\(

grep("\\(direct\\) / \\(none\\)", df$source)

或使用fixed = TRUE告诉grep按原样解释模式。

grep("(direct) / (none)", df$source, fixed = TRUE)
相关问题