如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的? [英] How to find the first most frequent, second most frequent, ..., last frequent in text?

查看:31
本文介绍了如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在下面的文本 cat 中找到第一个最常用的,第二个最常用的,...,最后一个最常用的词/类别.

I'm trying to find the first most frequent, the second most frequent, ..., the last most frequent words/categories in the following text cat.

library(stringr)
cat <- c("AA","AA","AA","Ee","Dd","Ee","Bb","Cc","Cc","Cc")

我需要的输出:

most1 AAA Cc
most2 Ee
most3 Bb Dd

在这方面有人可以帮我吗?Tnx!

Can one help me in this regard? Tnx!

推荐答案

你可以使用 table 像:

sort(table(cat), TRUE)
#cat
#AA Cc Ee Bb Dd 
# 3  3  2  1  1 

并作为字符向量:

x <- table(cat)
x <- rev(do.call(rbind, lapply(split(names(x), x), paste,collapse = " ")))
cbind(paste0("most", seq(x)), x)
#             x      
#[1,] "most1" "AA Cc"
#[2,] "most2" "Ee"   
#[3,] "most3" "Bb Dd"

变体:

x <- table(cat)
x <- do.call(rbind, rev(lapply(split(names(x), x), list)))
as.data.frame(cbind(paste0("most", seq(x)), x))
#     V1     V2
#3 most1 AA, Cc
#2 most2     Ee
#1 most3 Bb, Dd

这篇关于如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆