How to find the first most frequent, second most frequent, …, last frequent in text?
问题 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") OUTPUT that I need: most1 AAA Cc most2 Ee most3 Bb Dd Can one help me in this regard? Tnx! 回答1: You can use table like: sort(table(cat), TRUE) #cat #AA Cc Ee Bb Dd # 3 3 2 1 1 And as a character vector: x <- table(cat) x <- rev(do.call(rbind, lapply(split(names(x), x), paste