6

Unlike rowMeans() and rowMedians(), which give us the calculated figure, mode(x) gives the storage mode of the data.

My Question - For the following data frame, how can I calculate row wise Mode?

Data:

    Item       A    B   C
    Book001    56   32  56
    Book002    95   95  20
    Book003    50   89  50
    Book004    6    65  40

I am reading my worksheet like this:

wk= loadWorkbook (".....xls")
df = readWorksheet (wk, Sheet="Sheet1", header=TRUE)
Amrita Das
  • 91
  • 1
  • 5
  • @TimBiegeleisen : I have referred that page already! I am looking forward to finding Row wise mode when reading the input as data Frames from Excel. – Amrita Das May 19 '15 at 14:23
  • The answer given by @dimitris_ps is correct and is also very slim and efficient. – Tim Biegeleisen May 19 '15 at 14:33
  • @dimitris_ps 's answer is actually better than that given in the duplicate. I hope it will be marked correct so other developers can use this as a reference. – Tim Biegeleisen May 19 '15 at 14:36
  • 1
    @TimBiegeleisen, right, but the other is `base R` so they can "cohabit" ;-) – Cath May 19 '15 at 14:38

1 Answers1

9

Try this

install.packages("modeest")
library(modeest)

apply(df[ ,2:length(df)], 1, mfv)
dimitris_ps
  • 5,849
  • 3
  • 29
  • 55
  • 2
    Very nice. I never heard of this package before. – Tim Biegeleisen May 19 '15 at 14:28
  • 1
    If you are looking for a "Base R" solution, then have a look at [this SO post](http://stackoverflow.com/questions/2547402/standard-library-function-in-r-for-finding-the-mode) which answers a similar question. – Tim Biegeleisen May 19 '15 at 14:41
  • @dimitris_ps : However, this doesn't work incase my data doesn't have a "frequently occurring term" . How can i solve such a case? – Amrita Das May 20 '15 at 05:15