0

The goal is to retrieve a mode based on the column and its data.

data <- data.frame(test=c("a","b","c","a","a","a","a","c","c","c","c","c","c"))

Is there an R built-in function or standard way to calculate the mode of data$test?

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205
HelloWorld1
  • 13,688
  • 28
  • 82
  • 145

1 Answers1

2
table(data$test)

#a b c 
#5 1 7 


table(data$test)==max(table(data$test))

#    a     b     c 
#FALSE FALSE  TRUE 

or

names(table(data$tes)[table(data$test)==max(table(data$test))])
#[1] "c"
germcd
  • 954
  • 1
  • 12
  • 24
  • I don't understand. What does "max(table(data$test))" do? – HelloWorld1 Oct 12 '14 at 13:57
  • @FullMetalGame it's the number of times the mode occurs which in this case is 7 – germcd Oct 12 '14 at 14:03
  • 1
    @RichardScriven - `which.max()` does not handle solutions where there are multiple modes, which is not an issue here - but could very well be the case in other datasets. – Chase Oct 12 '14 at 16:06