1

I found a simple solution to getting the most frequent factor of a column in a dataframe using

names(which.max(table(df$column)))

but what if I wanted to find find the most frequent factor inside a chain. Is there a simple code that just gives you the 'mode' of the factors?

Or is there a method to include the above code inside a chain?

I've done it like this, which seems like a huge waste of time.

(df %>% group_by(column) %>% summarise(count=n()) %>% arrange(desc(count)))$count[1]

A simple code would be appreciated, no need to provide sample data. Thanks!

Ricky
  • 305
  • 3
  • 14
  • Why can't you just replace `df` by `.` in your first example? –  Mar 28 '17 at 22:07
  • I'm sorry I am not understanding, I'm very new to R. I just want to do this in a chain because I want to first filter and and mutate the df, then get the most frequent factor for a particular column. However I do not want to create a new df in the process, and then do the table method. – Ricky Mar 28 '17 at 22:12
  • `df %>% top_n(1, column)` – alistaire Mar 28 '17 at 22:22
  • @Ricky I meant, `df %>% mutate(new_column = names(which.max(table(.$column))))` –  Mar 28 '17 at 22:24
  • Also, [it's _your_ responsibility to provide sample data](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610) if you want an answer. – alistaire Mar 28 '17 at 22:40
  • My mistake alistaire. And I was also looking to return a single element rather than the df itself, but that answer is very useful for learning as well. – Ricky Mar 28 '17 at 22:56

1 Answers1

3

You could use the %$% infix operator from magrittr:

df %$% column %>% table %>% which.max %>% names

Or you could use the following syntax just using the pipe:

df %>% {.$column} %>% table %>% which.max %>% names

Or even:

df %>% `$`("column") %>% table %>% which.max %>% names
Nick Kennedy
  • 12,510
  • 2
  • 30
  • 52