Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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/4/regex/17.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
计算单词出现的次数(dplyr)_R_Regex_Count_Dplyr - Fatal编程技术网

计算单词出现的次数(dplyr)

计算单词出现的次数(dplyr),r,regex,count,dplyr,R,Regex,Count,Dplyr,这里有一个简单的问题,也许是重复的 我想知道如何计算一个单词在向量中出现的次数。我知道我可以计算单词出现的行数,如下所示: temp <- tibble(idvar = 1:3, response = (c("This sounds great", "This is a great idea that sounds great", "What a great idea")

这里有一个简单的问题,也许是重复的

我想知道如何计算一个单词在向量中出现的次数。我知道我可以计算单词出现的行数,如下所示:

temp <- tibble(idvar = 1:3, 
               response = (c("This sounds great",
                      "This is a great idea that sounds great",
                      "What a great idea")))
temp %>% count(grepl("great", response)) # lots of ways to do this line
# answer = 3
temp%count(grepl(“great”,response))#这行有很多方法
#答案=3

上面代码中的答案是3,因为“great”分三行显示。然而,“伟大”一词在向量“回应”中出现了4次。如何找到它?

我们可以使用
strungr
中的
stru count
来获得每行中具有“great”的实例数,然后获得该计数的
总和

library(tidyverse)
temp %>% 
   mutate(n = str_count(response, 'great')) %>%
   summarise(n = sum(n))
# A tibble: 1 x 1
#      n
#   <int>
#1     4

我们可以使用
stru count
from
stringr
来获得每行中具有“great”的实例数,然后获得该计数的
总和

library(tidyverse)
temp %>% 
   mutate(n = str_count(response, 'great')) %>%
   summarise(n = sum(n))
# A tibble: 1 x 1
#      n
#   <int>
#1     4

在我看来,这应该能解决你的问题:

library(tidyverse)
temp$response %>% 
  str_extract_all('great') %>%
  unlist %>%
  length

在我看来,这应该能解决你的问题:

library(tidyverse)
temp$response %>% 
  str_extract_all('great') %>%
  unlist %>%
  length

您是否计划提供一个特定的单词并获得您想要的号码?或者你想得到所有句子中出现的每个单词的数字?只是计划提供一个特定的单词并得到数字。我可以使用
tidytext
unnest将句子分割成标记,然后计算单词数。(但如果你有其他方法的建议,我洗耳恭听!)我也想到了
tidytext
:)你打算提供一个特定的单词并得到你想要的号码吗?或者你想得到所有句子中出现的每个单词的数字?只是计划提供一个特定的单词并得到数字。我可以使用
tidytext
unnest将句子分割成标记,然后计算单词数。(但如果你有其他方法的建议,我洗耳恭听!)我也考虑了
tidytext
:)感谢添加了
base R
——这在我的一些用例中实际上可能更简单。感谢添加了
base R
——这在我的一些用例中实际上可能更简单。