0

Hi I am trying to work out the mode for each unique ID.

In other words, I would like to know the mode for each household_key for DAY, TRANS_TIME, WEEK_NO. i.e. on which day each household appears the most etc.

I have put together some code

library(dplyr)
randomtest <- mydata %>% group_by(household_key) %>% 
  summarise_each(funs(mode),DAY, TRANS_TIME, WEEK_NO)

This outputs:

  household_key     DAY TRANS_TIME WEEK_NO
1              1 numeric    numeric numeric
2              2 numeric    numeric numeric
3              3 numeric    numeric numeric
4              4 numeric    numeric numeric
5              5 numeric    numeric numeric
6              6 numeric    numeric numeric
7              7 numeric    numeric numeric
8              8 numeric    numeric numeric
9              9 numeric    numeric numeric
10            10 numeric    numeric numeric

I understand numeric means decimals? Does that mean my answers are in decimals if so how I can change it to round up the value and display me a number?

Below is sample of my dataframe <- mydata

household_key   DAY TRANS_TIME  WEEK_NO
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
2375    1   6   1
1364    1   6   1
1364    1   6   1
1364    1   6   1
1364    1   6   1
1364    1   6   1
1130    1   5   1
1130    1   5   1
1130    1   5   1
1130    1   5   1
1130    1   5   1
1173    1   7   1
1173    1   7   1
1173    1   7   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
1172    1   4   1
user2704941
  • 13
  • 1
  • 3
  • 7
  • You should look at `?mode` and see that it's not the function you're looking for. See the duplicate question for many, many options. – Gregor Thomas Mar 08 '15 at 20:22
  • @Gregor I have seen the link before i posted this Question, I am new to R and was unable to relate any of the answers to what I am trying to achieve? Could you please give me some suggestions on how to do this? – user2704941 Mar 08 '15 at 20:24
  • Pick one, e.g., the accepted answer from Ken Williams. Run his code that defines the function `Mode`. Then run the code in your question replacing `funs(mode)`,which gives the storage mode of the object, with `funs(Mode)`, which gives the most frequent observation. – Gregor Thomas Mar 08 '15 at 20:31
  • Mode <- function(x) { ux <- unique(x) ux[which.max(tabulate(match(x, ux)))] } library(dplyr) randomtest <- mydata %>% group_by(household_key) %>% summarise_each(funs(Mode),DAY, TRANS_TIME, WEEK_NO) View(randomtest) – user2704941 Mar 08 '15 at 20:34

1 Answers1

1

Here's another approach:

smode <-function(x){
    xtab<-table(x)
    modes<-xtab[max(xtab)==xtab]
    mag<-as.numeric(modes[1]) #in case mult. modes, this is safer
    themodes<-names(modes)
    mout<-list(themodes=themodes,modeval=mag)
    return(mout)
    }
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73