Find the most frequent value in a column and take a subset of that

I have the following dataset.

Num              Day              Value
1111             Thursday         2
2222             Thursday         2
3333             Thursday         3
1111             Monday           3
1111             Tuesday          3
1111             Wednesday        3
1111             Friday           4
1111             Saturday         5
1111             Sunday           6
2222             Thursday         6

      

We can get the highest value in a column value

using the following command:tail(names(sort(table(data$value))), 1)

I need to dynamically get a subset of the data containing only that highest value.

Thus, I need the following output:

Num              Day              Value
3333             Thursday         3
1111             Monday           3
1111             Tuesday          3
1111             Wednesday        3

      

+3


source to share


1 answer


you can use subset

 indx <- tail(names(sort(table(df1$Value))),1)
 subset(df1, Value==indx)

      

Or using dplyr



 library(dplyr)
 df1 %>% 
   group_by(Value) %>% 
   mutate(N=n()) %>%
   ungroup() %>% 
   filter(N==max(N))

      

or

  library(data.table)
  setDT(df1)[, N:=.N, Value][N==max(N)][, N:=NULL]

      

+5


source







All Articles