I can't compute the mode

Hello,
I need to compute the mode using tidyverse.
If I run the code below, I get results:

calculate_mode <- function(x) {
  tbl <- table(x)
  modes <- as.numeric(names(tbl[tbl == max(tbl)]))
  if (length(modes) == length(x)) {
    return(NA)
  } else {
    return(modes)
  }
}



set.seed(123)  # Establecer una semilla para reproducibilidad

nombres_paises <- sample(c("Argentina", "Brasil", "Chile", "Colombia", "México", "Perú", "España", "Francia", "Italia", "Alemania"), 2000, replace = TRUE)

colores <- sample(c("Rojo", "Verde", "Azul", "Amarillo"), 2000, replace = TRUE)

numeros_aleatorios_1 <- sample(10:50, 2000, replace = TRUE)
numeros_aleatorios_2 <- sample(10:50, 2000, replace = TRUE)

datos <- data.frame(
  pais = nombres_paises,
  color = colores,
  numero1 = numeros_aleatorios_1,
  numero2 = numeros_aleatorios_2
)

datos %>% glimpse()

datos %>%
  group_by(pais) %>% 
  summarise(across(where(is.numeric), list(
    promedio = ~mean(., na.rm = TRUE),
    Minimo = ~min(., na.rm = TRUE),
    Maximo = ~max(., na.rm = TRUE),
    Moda = ~calculate_mode(.),
    Desv = ~sd(., na.rm = TRUE)
  ), .names = "{col}_{fn}")) %>% View()

As you can see, I obtained the way to compute the mode using a function (credits to chatgpt).
But when I try to use the syntax in some data, I received:

Error in View : subscript out of bounds

I'm grouping using a character variable consisting of 32 categories.
The numeric variables are 13 (all of them have a range from 0 to 5).
I think the function can't handle so many intersections. But that's a guess.
Thanks for your time and interest, folks.
Have a nice week.

See Mode() in {DescTools}

I would not put a lot of faith in ChatGPT. I have seen a couple of reports that suggest it writes pretty R code but the code is often wrong.

This should be your reference if you don't want to use an additional package:
r - How to find the statistical mode? - Stack Overflow

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.