Frequency of values per column in table

前端 未结 4 1047
傲寒
傲寒 2020-12-21 07:59

What is a good way to get the independent frequency counts of multiple columns using dplyr? I want to go from a table of values:

# A tibble: 7          


        
4条回答
  •  太阳男子
    2020-12-21 08:18

    For the same data set that you provided in the question this would be another solution (base-R):

    myfreq <- sapply(df, function(x) table(factor(x, levels=unique(unlist(df)), ordered=TRUE)))
    

    Output would be:

    > myfreq
    
    #   a b c d 
    # 1 2 0 2 1 
    # 2 1 4 2 0 
    # 3 2 2 0 6 
    # 5 2 0 1 0 
    # 4 0 1 2 0
    

提交回复
热议问题