这是我的数据集
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))
答案 0 :(得分:3)
(
有a special meaning in regex。你应该逃避它\\(
grep("\\(direct\\) / \\(none\\)", df$source)
或使用fixed = TRUE
告诉grep
按原样解释模式。
grep("(direct) / (none)", df$source, fixed = TRUE)