Get most frequent string from a data frame column

前端 未结 2 397
长发绾君心
长发绾君心 2021-01-20 02:58

I need to return the n most frequent occurrences of a string, using a multiple row data frame as the input. All the values are in the same column called \"MissingDates\"

2条回答
  •  渐次进展
    2021-01-20 03:29

    It seems like you need something like:

    Function

    freqfunc <- function(x, n){
      tail(sort(table(unlist(strsplit(as.character(x), ", ")))), n)
    }
    

    Testing on your data set

    freqfunc(gaps$MissingDates, 5) # Five most frequent dates
    
    ## 1996-12-26 1997-12-26 1998-01-02 1999-12-31 2001-09-12 
    ##          4          4          4          4          4 
    

提交回复
热议问题