本文介绍了如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在下面的文本 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更多推荐
如何在文本中找到第一个最频繁的,第二个最频繁的,...,最后一个频繁的?
发布评论