Frequency count of two column in R

后端 未结 7 898
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-27 03:37

I have two columns in data frame

2010  1
2010  1
2010  2
2010  2
2010  3
2011  1
2011  2

I want to count frequency of both columns and get

相关标签:
7条回答
  • 2020-11-27 04:14

    I haven't seen a dplyr answer yet. The code is rather simple.

    library(dplyr)
    rename(count(df, y, m), Freq = n)
    # Source: local data frame [5 x 3]
    # Groups: V1 [?]
    #
    #       y     m  Freq
    #   (int) (int) (int)
    # 1  2010     1     2
    # 2  2010     2     2
    # 3  2010     3     1
    # 4  2011     1     1
    # 5  2011     2     1
    

    Data:

    df <- structure(list(y = c(2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 
    2011L), m = c(1L, 1L, 2L, 2L, 3L, 1L, 2L)), .Names = c("y", "m"
    ), class = "data.frame", row.names = c(NA, -7L))
    
    0 讨论(0)
提交回复
热议问题