Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can r show me the mode and how often the mode appears?

Tags:

r

I have a dataset containing votes as columns and parliamentarians as rows. I want to compute an agreement index and therefore need the frequency of the mode.

A column looks e.g. like this

V1
1
3
2
1
1
2
1

I know the following code to show me the mode

getmode <- function(v) {
  uniqv <- unique(v)
  uniqv[which.max(tabulate(match(v, uniqv)))]
}

And I know how I R shows me the frequency of values

a <- table(df$V1)
print(a)

Is there a way that R takes the mode, in my example 1, and shows me how frequent it is, in my example 4?

like image 693
Alexander Klein Avatar asked Jan 23 '26 18:01

Alexander Klein


1 Answers

You can just do

a <- table(df$V1)
max(a)

Or using your getmode function

sum(df$V1 == getmode(df$V1))
like image 72
mickey Avatar answered Jan 25 '26 07:01

mickey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!