Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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
增强R中的嵌套循环性能_R - Fatal编程技术网

增强R中的嵌套循环性能

增强R中的嵌套循环性能,r,R,我在R中循环两个数据帧,第一个数据帧包含单词列表,第二个数据帧包含段落列表 我的任务是计算每个段落中存在的dataframe1中的字数,并将它们存储在dataframe2的Count列中 我使用嵌套for循环完成了这项工作,但它需要大量的时间 有没有办法提高下面代码的性能 for (i in 1:nrow(words)) { for(j in 1:nrow(paragraphs)) { if(grepl(words[i,1],paragraphs[j,1])) {

我在R中循环两个数据帧,第一个数据帧包含单词列表,第二个数据帧包含段落列表

我的任务是计算每个段落中存在的dataframe1中的字数,并将它们存储在dataframe2的Count列中

我使用嵌套for循环完成了这项工作,但它需要大量的时间

有没有办法提高下面代码的性能

for (i in 1:nrow(words))
{  
  for(j in 1:nrow(paragraphs))
  {
    if(grepl(words[i,1],paragraphs[j,1]))
    {
      paragraphs[j,"count"]=paragraphs[j,"count"]+1
    }
  }
}
更新:

示例词:

折磨

咄咄逼人

烦人的

示例段落:

手机太高、太笨重、太烦人,无法忍受使用

我唯一担心的是,如果伯尼·桑德斯是总统,他可能会死在办公室里

伯尼·桑德斯可能关心这些问题,但他真的理解它们吗?这就是你可以做的问题:

words <- data.frame(w=c("afflict", "afraid", "aggressive", "annoying"))
paragraphs <- data.frame(p=c("the phone will be too tall and bulky and annoying to tolerate use", 
                             "the only thing that i am afraid of if bernie sanders is president he may die in office", 
                             "Bernie Sanders may care about the issues, but does he really understand them? That is the question"))
paragraphs$count <- rowSums(sapply(words[,1], grepl, x=paragraphs[,1], fixed=TRUE))

@989更新,我添加了示例数据添加数据时,您应该添加代码来生成数据。使用dput。您呈现数据的方式意味着我们应该花时间生成data.frames并测试代码。