0

Odd I couldn't find a similar question in stackoverflow.

Suppose I have

test1 <- list("1" = c(1, 2, 3), "2" = c(1, 2, 3), "3" = c(4, 5, 6))
want2 <- c(1, 2, 3)

test2 <- list("1" = iris, "2" = iris, "3" = mtcars)
want2 <- iris

How many I find the most frequent element in a list?

aiorr
  • 547
  • 4
  • 11
  • 1
    @IceCreamToucan, they want a more complex mechanism that takes into account a whole frame, not individual numbers. I suggest you un-dupe it. – r2evans Dec 20 '21 at 15:18
  • 2
    Try the function in the first answer. It gives the desired output @r2evans. Well, I guess to exactly match you'd have to add a `[[1]]` to the end but I don't think that's a meaningful difference between the questions. – IceCreamToucan Dec 20 '21 at 15:18
  • Odd ... I didn't know that `unique` (and others) worked on structures like that. Thanks. – r2evans Dec 20 '21 at 15:20
  • @IceCreamToucan thank you for the suggestion. Perhaps I am not setting up the optional arguments properly, but `collapse::fmode(test2)` returns `Not supported SEXP type !` error. The vignette also doesn't seem to tell me that it supports list, only dataframe. Would you kindly help me out? – aiorr Dec 20 '21 at 15:25
  • 1
    The `Mode` function in Ken Williams' answer should work. Maybe when working with lists, a slight modification would be more appropriate: `Mode2 <- function(x) { x[which.max(tabulate(match(x, x)))][[1]] }` – IceCreamToucan Dec 20 '21 at 15:28
  • 1
    @IceCreamToucan Thank you, I'm not sure why my browser did not display an accepted answer as first answer. – aiorr Dec 20 '21 at 15:33

0 Answers0