我是新手,正在尝试检测字符串变量中的字母C OR C / OR C
x <- c("C", "C/", "C,", "C++", "C#", "Captain")
所需的输出
True, True, True, False, False, False
尝试过此操作,但选择了所有大写字母,但“队长”除外
str_detect(x, "[C]")
非常感谢任何帮助。
答案 0 :(得分:2)
如何使用grepl
:
grepl("(^| )C[/,]?( |$)", input)
[1] "The C programming language" "The C/ programming language"
[3] "The C, programming language"
数据:
input <- c("The C programming language",
"The C/ programming language",
"The C, programming language",
"The C++ programming language",
"The C# programming language",
"Captain of all")
编辑:
基于更新后的预期输出,也许这就是您想要的:
grepl("^C[/,]?$", x)
答案 1 :(得分:1)
感谢你们俩。这段代码终于对我有用:
str_detect(input, "(^| )C[/,]?( |/|$)")