微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 group_by 计算 R 中 2 个数据帧之间的特定单词出现

如何解决使用 group_by 计算 R 中 2 个数据帧之间的特定单词出现

我在 R 中有两个数据框,第一个(命名为 Words)由单列单词组成:

你好
建筑
学校
医院
医生

第二个是一个大数据集,如下所示:

id 描述
382 建设学校
787 为新医院招聘医生,为学校招聘教师

然后,我想按ID分组并获得以下结果

id 描述 匹配
382 建设学校 2
787 为新医院招聘医生,为学校招聘教师 3

这是我试过的

library(stringr)

df <- df %>% group_by(df$id)

getCount <- function(data,keyword)
{
  wcount <- str_count(df$description,keyword)
  return(data.frame(data,wcount))
}

gCount(df$description,Words)

(我也尝试过将 Words 数据集转换为列表)

还有:

df <- df %>% group_by(df$id)
table(df$description)

df$match <- df[df$description %in% Words$Words,]
table(df$match)

最后


Words.list <- setNames(split(Words,seq(nrow(Words))),rownames(Words))
description <- subset(df,select = c("description","id"))
description <- description %>% group_by(description$id)
description.list <- setNames(split(description,seq(nrow(description))),rownames(description))

str_to_search = Words.list
str_to_count = description.list

lengths(regmatches(str_to_search,gregexpr(str_to_count,str_to_search,fixed = TRUE)))

然而,我只有一些我不明白的奇怪错误信息。

解决方法

library(stringr)
library(purrr)

words <- c("Hello","Building","School","Hospital","Doctors") %>%
  str_to_lower()
descriptions <- c("Building a school","Hiring doctors for the new hospital and teachers for the school") 

df_descriptions <- data.frame(description = descriptions) %>%
    mutate(Match = map_int(str_to_lower(description),~str_count(.x,words) %>% sum()))

编辑

df_descriptions <- data.frame(description = descriptions) %>%
  mutate(
    Match = str_to_lower(description) %>%
      str_split(" ") %>%
      map_int(~sum(.x %in% words))
  )

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。