1

I've run this code

var <- c("A","A","A","A","B","B","B","B","B","B","C","C","C")
table(var)
> table(var)
var
A B C 
4 6 3 

The maximum frequency is 6, for factor "B". Is there a function that just returns the name of the factor which has the highest frequency, "B". Any help greatly appreciated. Thanks

0

2 Answers 2

1

A possible solution:

library(tidyverse)

var <- c("A","A","A","A","B","B","B","B","B","B","C","C","C")
table(var) %>% which.max %>% names

#> [1] "B"

In base R:

names(which.max(table(var)))
Sign up to request clarification or add additional context in comments.

Comments

1

Using tidyverse:

library(tidyverse)

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

df <- tibble(var = var)

df %>% 
  count(var,sort = TRUE) %>% 
  slice(1) %>% 
  pull(var)
#> [1] "B"

Created on 2021-11-17 by the reprex package (v2.0.1)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.