2016-11-09 30 views
0

这是我的数据集不grep的使用功能工作“(”

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有什么不同?这是否是“(”?

这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)) 
+2

你可以使用'grep'跳过所有,如果只是寻找一个直接匹配'DF [DF $源==“(直接)/(无)”]' – thelatemail

+0

对不起也许我的例子不是易洁我想要所有包含“(直接)/(无)”的行。例如,如果一个用户的源是l,(直接)/(无),a,b,c,我也需要这个 – MFR

回答

3

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

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

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

grep("(direct)/(none)", df$source, fixed = TRUE) 
+0

谢谢。第一个选项并不完美,我通过使用第二个选项得到了这个错误:“unused argument(fixed = TRUE)” – MFR

+0

你把'fixed = TRUE'放在正确的位置吗?这绝对是'grep'的一个参数。 – Hugh