Is there an R function that helps me locate the fashions of a multimodal dataset?

Asked

Viewed 223 times

2

Something like Encontramoda(Dataset, N_maiores_modas)?

I am working with the following integers: c(1,2,3,4,5,6,7,7,7,8,8,8,9,9,9,9)

That is, the example is trimodal and has a bigger fashion than the other two.

The ideal would be perhaps to create a function that puts all the integers and order and subtracts the next one from the previous one, when giving 0 sum to a fashion variable N and then comparing these fashion variables N? I’m trying to develop the code for this but I still can’t.

  • In his example, the fashion is 9, since it occurs more often than 7 and 8.

1 answer

5

Correcting the function of this site, https://www.r-bloggers.com/lang/portuguese/517 :

statmod <- function(x) {
  z <- table(as.vector(x))
  names(z)[z == max(z)]
}

a <- c(1,2,3,4,5,6,7,7,7,8,8,8,9,9,9,9)

statmod(a)

a
[1] "9"

Having 7, 8 and 9 with the same amount of repetitions, ie, same fashion:

b <- c(1,2,3,4,5,6,7,7,7,8,8,8,9,9,9)

statmod(b)

b
[1] "7" "8" "9"

If you want to know the count of each repetition, use neatly:

sort(table(a), decreasing = TRUE)
  • 1

    Should not be table(as.vector(x))? The function is finding the a because it goes looking out of its Environment and finds in the globalenv.

  • Correct Rui! Thank you!

  • Perfect Daniel! For small sets it works great, but if I have 40,000 items, for example, and need to know which 10 items repeat the most? How would I subtract the vector fashion to run statmod again? Would that be it? y <-x - rep(a) statmod(y) y Or I would change the statmod to: statmod <- Function(x,y) { w = 0 for w in y { z <- table(as.vector(x)) Names(z)[z == max(z)] x - z } &##;?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.