如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的?

编程入门 行业动态 更新时间:2024-10-08 04:33:18
本文介绍了如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试在下面的文本 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

更多推荐

如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的?

本文发布于:2023-11-29 09:26:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1646001.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:频繁   第一个   第二个   如何在   文本中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!