-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
  • 103,777
  • 62
  • 306
  • 498
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
  • 178,638
  • 15
  • 253
  • 268
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
  • 48,205
  • 9
  • 52
  • 84