Get most frequent string from a data frame column

前端 未结 2 365
长发绾君心
长发绾君心 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:20

    Here's another possible solution.

    Set up some data,

    set.seed(5)
    ss1 <- sample(seq(s <- Sys.Date(), s+10, "day"), 20, TRUE)
    ss2 <- sample(seq(s <- Sys.Date(), s+10, "day"), 20, TRUE)
    ls1 <- list(ss1 = ss1, ss2 = ss2)
    

    Define the function:

    f <- function(x, n) sort(table(x), decreasing = TRUE)[1:n]
    

    Apply the function over the data:

    lapply(ls1, f, n = 3)
    # $ss1
    # x
    # 2014-09-08 2014-09-09 2014-09-07 
    #          3          3          2 
    # 
    # $ss2
    # x
    # 2014-09-10 2014-09-06 2014-09-07 
    #          4          3          2 
    

提交回复
热议问题