将 grep 与 count_if 一起使用(R 中的 EXPSS 包)

using grep with count_if (EXPSS package in R)

我正在尝试计算某个字符串出现在数据框中的实例(这将是一个子字符串,即 "blue" 将出现在更大的文本块中),然后总结这些计数由另一个领域。这是代码:

    totals_by_county <- county_data %>%
      group_by(county_data$county)%>%
      summarise(number_occurences = count(grepl('blue', county_data$color,ignore.case = TRUE))) 
    totals_by_county

我收到这个错误:

  no applicable method for 'summarise_' applied to an object of class "logical"

有没有办法在我上面尝试使用的方法中做到这一点?提前致谢!

grepl:

totals_by_county <- county_data %>%
    group_by(county) %>%
    summarise(number_occurences = sum(grepl('blue', color, ignore.case = TRUE))) 

或者,count_if 来自 expss

totals_by_county <- county_data %>%
    group_by(county) %>%
    summarise(number_occurences = count_if(perl('blue', ignore.case = TRUE), color)) 

用可重现的例子更新:

library(dplyr)
library(expss)

county_data = data.frame(
    county = c("A", "A", "A", "B", "B"),
    color = c("blue-blue", "red", "orange-blue", "yellow", "green"),
    stringsAsFactors = FALSE)


county_data %>%
    group_by(county) %>%
    summarise(number_occurences = count_if(perl('blue', ignore.case = TRUE), color)) 

# A tibble: 2 x 2
# county number_occurences
# <chr>              <int>
# 1 A                  2
# 2 B                  0