What function do I need to use to rank first four most popular words in list in R program?
For example,
c("apple", "banana", "apple", "banana", "banana",
"desk", "pen", "pen", "pen", "pen")
to make it like
"pen"
"banana"
"apple"
"desk"
Thank you
You can sort the tabled values in decreasing order and then take the names to get the output you're looking for. Try this:
> x <- c("apple", "banana", "apple", "banana", "banana",
"desk", "pen", "pen", "pen", "pen")
> names(sort(table(x), decreasing = TRUE))
## [1] "pen" "banana" "apple" "desk"
table
is intuitive for base R. Here's a qdap approach:
library(qdap)
freq_terms(dat, 4)
## WORD FREQ
## 1 pen 4
## 2 banana 3
## 3 apple 2
## 4 desk 1
Or...
freq_terms(dat, 4)[, 1]
## [1] "pen" "banana" "apple" "desk"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With