-1

How do I count how many times does a word appear in R and the output is the one which appears the most?

a <- list(c("A", "A", "A", "A", "B", "B", "A", "B", "C", "C", "C", "A"))

the output should be "A"

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
A. Smith
  • 39
  • 2

2 Answers2

1

Not sure if you really have a list or vector, but with a vector

a <-c("A", "A", "A", "A", "B", "B", "A", "B", "C", "C", "C", "A")

you can do

names(sort(table(a), decreasing=TRUE))[1]

to get the most common value

MrFlick
  • 195,160
  • 17
  • 277
  • 295
1

You can use sort with the decreasing=TRUE flag:

sort(table(list(c("A", "A", "A", "A", "B", "B", "A", "B", "C", "C", "C", "A"))),decreasing=TRUE)[1]

Output:

A 
6 
user3483203
  • 50,081
  • 9
  • 65
  • 94