使用grep和count_if(R中的EXPSS包)

eh57zj3b  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(78)

我试图计算某个字符串出现在 Dataframe 中的示例(这将是一个子字符串,即“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

我得到这个错误:
“summarise_”没有适用的方法应用于类“logical”的对象
在我上面尝试使用的方法中有没有方法可以做到这一点?

rta7y2nd

rta7y2nd1#

grepl

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

或者,将count_ifexpss转换为:

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

相关问题