首页 文章

计算R中出现的次数

提问于
浏览
1

对于示例数据框:

df <- structure(list(area = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"), 
                      count = c(1L, 1L, 1L, 3L, 4L, 2L, 2L, 4L, 2L, 5L, 6L)), 
                 .Names = c("area", "count"), class = c("tbl_df", "tbl", "data.frame"), 
                 row.names = c(NA, -11L), spec = structure(list(cols = structure(list(area = structure(list(), 
                 class = c("collector_character", "collector")), count = structure(list(), class = c("collector_integer",
                 "collector"))), .Names = c("area", "count")), default = structure(list(), class = c("collector_guess", 
                "collector"))), .Names = c("cols", "default"), class = "col_spec"))

...列出了每个区域出现的事件数量,我希望生成另一个汇总表,显示有多少个区域有一次出现,两次出现,三次出现等 . 例如,有三个区域“每个区域出现一次” “,”每个区域有两次出现“的三个区域,一个区域”每个区域有三次出现“等 .

什么是产生我想要的结果的最佳包/代码?我尝试过聚合和plyr,但到目前为止还没有成功 .

3 回答

  • 2

    我喜欢data.table语法

    library(data.table)
    setDT(df) # transform data.frame into data.table format
    
    # .N calculates the number of observations, by instance of the count variable
    df[, .(n_areas = .N), by = count]
    
       count n_areas
    1:     1       3
    2:     3       1
    3:     4       2
    4:     2       3
    5:     5       1
    6:     6       1
    

    请参阅此问题,以便比较最常用于此类操作的两个大包: dplyrdata.table data.table vs dplyr: can one do something well the other can't or does poorly?

  • 1

    您可以使用基本R功能:使用@Jimbou解决方案

    table(df$count)
    1 2 3 4 5 6 
    3 3 1 2 1 1
    
  • 2

    使用精彩的 dplyr 库非常直观 .

    首先,我们使用 count 的唯一值对数据进行分组,然后使用 n() 计算每个组的出现次数 .

    library(dplyr)
    df %>%
        group_by(count) %>%
        summarise(number = n())
    
    # A tibble: 6 x 2
      count number
      <int>  <int>
    1     1      3
    2     2      3
    3     3      1
    4     4      2
    5     5      1
    6     6      1
    

相关问题