【发布时间】:2019-07-26 08:41:13
【问题描述】:
我在 R 中使用 grep/grepl 函数时遇到问题。当我为多个字符串运行 grepl 时,我不知道如何生成所需的结果。
Text1 <- c("instance", "percentage", "n",
"instance percentage", "percentage instance")
ID <- c(1,2,3,4,5)
A <- c("A","B","C","D","E")
df <- data.frame(ID, Text1, A)
我想找到字符串实例或百分比(或两者兼而有之,并添加另一列“结果”并在每次找到时给出 1。
结果如下所示:
ID Text1 A Result
1 instance A 1
2 percentage B 1
3 n C
4 instance percentage D 1
5 percentage instance E 1
【问题讨论】:
-
试试
grepl('instance|percentage', df$Text1)*1
标签: r