Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex R-在2个对象上循环_Regex_R_Loops_For Loop_Grepl - Fatal编程技术网

Regex R-在2个对象上循环

Regex R-在2个对象上循环,regex,r,loops,for-loop,grepl,Regex,R,Loops,For Loop,Grepl,我正试图做一些相当简单的事情(我想),但我无法控制它。我试图编写一个循环,检查数据帧中的字符变量是否包含任何特定的子字符串列表,并为伪变量指定相应的值 因此,假设一个data.frame,n=2000,带有一个变量data.frame$text。此外,我还有一个字符向量,其中包含我要为其设置textdata.frame$text的所有子字符串。让我们称之为“希拉里存在”: hillary_exists <- c("Hilary Clinton", "hilary clinton","hil

我正试图做一些相当简单的事情(我想),但我无法控制它。我试图编写一个循环,检查数据帧中的字符变量是否包含任何特定的子字符串列表,并为伪变量指定相应的值

因此,假设一个data.frame,n=2000,带有一个变量
data.frame$text
。此外,我还有一个字符向量,其中包含我要为其设置text
data.frame$text
的所有子字符串。让我们称之为“希拉里存在”:

hillary_exists <- c("Hilary Clinton", "hilary clinton","hilaryclinton", "hillaryclinton", "HilaryClinton",
                    "HillaryClinton","Hillary Clinton", "Hillary Rodham Clinton", "Hillary", "Hilary", "#Hillary2016", "#ImWithHer",
                    "Hillary2016", "hillary", "hilary", "Clinton 2016", "Clinton", "Secretary of State Clinton", 
                    "Senator Clinton", "Hilary Rodham", "Hilary Rodham Clinton", "Hilary Rodham-Clinton", "Hillary Rodham-Clinton")
但是显然我缺少了
data.frame$text
元素的
I
组件,但我不知道如何处理它


任何帮助都将不胜感激!谢谢

我们可以使用的一种方法是将hillary_exists转换为正则表达式:
Hi Avinash Hi-Avinash,谢谢你的评论,你能详细说明一下吗?据我所知,这就是我所做的,不是吗?像这样,
data.frame$hillary\u-提提[I]关于
data.frame$hillary\u-提提@brittenb,也许你是对的,但我不清楚算法的预期行为是什么@nikUoM,比方说,我们有
hillary\u存在。在我看来,这段代码可能会给出正确的解决方案:
data.frame$hillary\u
for(i in hillary_exists){
  if(grepl(hillary_exists[i], data.frame$text)){
    data.frame$hillary_mention <- 1
  } else {
    data.frame$hillary_mention <- 0 }
}
data.frame$hillary_mention <- sapply(data.frame$text, function(s) grepl(hillary_regex, s, ignore.case = TRUE))