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"
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"
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
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