在 R 中按模式对数据帧进行分组

问题描述 投票:0回答:4

我有数百行的 R 数据框

word        Freq
seed         4
seeds        3
contract     2
contracting  2
river        1

我想按模式对数据进行分组,比如种子+种子......看起来像

word     Freq
seed      7
contract  4
river     1
r pattern-matching aggregate
4个回答
3
投票

这可能是另一种方法。在

SnowballC
包中,有一个函数可以清理单词并获取词干(即
wordStem()
)。我认为,使用它,您可以跳过字符串操作。完成此过程后,您所做的就是获得词频总和。

library(SnowballC)
library(dplyr)

mydf <- read.table(text = "word        Freq
seed         4
seeds        3
contract     2
contracting  2
river        1", header = T)

mutate(mydf, word = wordStem(word)) %>%
group_by(word) %>%
summarise(total = sum(Freq))

#      word total
#     (chr) (int)
#1 contract     4
#2    river     1
#3     seed     7

2
投票

一种选择是通过根据“word”中的最小字符数提取子字符串来创建分组变量“gr”,使用“word”sp再执行此操作,我们可以获得每组单词的子字符串,并且然后通过“word”获取“Freq”的

sum

library(dplyr)
 df1 %>% 
    group_by(gr= substr(word, 1, min(nchar(word)))) %>%
    group_by(word= substr(word, 1, min(nchar(word)))) %>%
    summarise(Freq= sum(Freq)) 
    word  Freq
#      (chr) (int)
#1 contract     4
#2    river     1
#3     seed     7

1
投票

也可以用cross join来做,比上面的方法安全一点。

library(dplyr)
library(stringi)

df %>%
  merge(df %>% select(short_word = word) ) %>%
  filter(short_word %>%
           stri_detect_regex(word, .) ) %>%
  group_by(word) %>%
  slice(short_word %>% stri_length %>% which.min) %>%
  group_by(short_word) %>%
  summarise(Freq= sum(Freq)) 

1
投票

尝试使用

adist
来匹配术语。

dat$grp <- seq(nrow(dat))

# generate a matrix comparing the vector of words to themselves
tmp <- adist(dat$word, dat$word, partial=TRUE)
diag(tmp) <- Inf
dat$grp[col(tmp)[tmp==0]] <- row(tmp)[tmp==0]

final <- aggregate(Freq ~ grp, data=dat, sum)
final$word <- dat$word[match(final$grp, dat$grp)]

#  grp Freq     word
#1   1    7     seed
#2   3    4 contract
#3   5    1    river

使用数据:

dat <- data.frame(word=c("seed","seeds","contract","contracting","river"),Freq=c(4,3,2,2,1))
© www.soinside.com 2019 - 2024. All rights reserved.