代码之家  ›  专栏  ›  技术社区  ›  clemens

在data.frame中筛选前n个最大组

r
  •  0
  • clemens  · 技术社区  · 6 年前

    对于示例数据:

    set.seed(2222)
    example_data <- data.frame(col1 = 1:15,
                               col2 = 16:30, 
                               group = sample(1:3, 15, replace = TRUE))
    
       col1 col2 group
    1     1   16     2
    2     2   17     1
    3     3   18     3
    4     4   19     2
    5     5   20     3
    6     6   21     1
    7     7   22     3
    8     8   23     1
    9     9   24     3
    10   10   25     1
    11   11   26     2
    12   12   27     2
    13   13   28     2
    14   14   29     3
    15   15   30     3
    

    我想找出记录数最多的前n组。

    假设我想获得最多记录的前两组。在数据中,这是第3组和第2组:

    example_data %>% 
      group_by(group) %>% 
      summarise(n = n())
    
    # A tibble: 3 x 2
      group     n
      <int> <int>
    1     1     4
    2     2     5
    3     3     6
    

    预期产出为:

       col1 col2 group
    1     1   16     2
    2     3   18     3
    3     4   19     2
    4     5   20     3
    5     7   22     3
    6     9   24     3
    7    11   26     2
    8    12   27     2
    9    13   28     2
    10   14   29     3
    11   15   30     3
    
    0 回复  |  直到 6 年前
        1
  •  4
  •   Ronak Shah    6 年前

    我们可以利用 table 计算每个 group 我是说, sort 他们进来了 decreasing 排序,将前两个条目子集 filter 各自的小组。

    library(dplyr)
    
    example_data %>%
       filter(group %in% names(sort(table(group), decreasing = TRUE)[1:2]))
    
    
    #   col1 col2 group
    #1     1   16     2
    #2     3   18     3
    #3     4   19     2
    #4     5   20     3
    #5     7   22     3
    #6     9   24     3
    #7    11   26     2
    #8    12   27     2
    #9    13   28     2
    #10   14   29     3
    #11   15   30     3
    

    你也可以直接用这个 subset

    subset(example_data, group %in% names(sort(table(group), decreasing = TRUE)[1:2]))
    
        2
  •  3
  •   akrun    6 年前

    我们可以利用 tidyverse 方法。创建频率列 add_count 我是说, arrange 在那个专栏和 filter “组”在最后两行中的行 unique “组”值

    library(dplyr)
    example_data %>% 
       add_count(group) %>% 
       arrange(n) %>%
       filter(group %in% tail(unique(group), 2)) %>%
       select(-n)
    # A tibble: 11 x 3
    #    col1  col2 group
    #  <int> <int> <int>
    # 1     1    16     2
    # 2     4    19     2
    # 3    11    26     2
    # 4    12    27     2
    # 5    13    28     2
    # 6     3    18     3
    # 7     5    20     3
    # 8     7    22     3
    # 9     9    24     3
    #10    14    29     3
    #11    15    30     3
    

    或使用 data.table

    library(data.table)
    setDT(example_data)[group %in% example_data[, .N, group][order(-N), head(group, 2)]]
    
        3
  •  2
  •   tmfmnk    6 年前

    dplyr ,您还可以执行以下操作:

    example_data %>%
     add_count(group) %>%
     filter(dense_rank(desc(n)) <= 2) %>%
     select(-n)
    
       col1  col2 group
       <int> <int> <int>
     1     1    16     2
     2     3    18     3
     3     4    19     2
     4     5    20     3
     5     7    22     3
     6     9    24     3
     7    11    26     2
     8    12    27     2
     9    13    28     2
    10    14    29     3
    11    15    30     3
    

    或:

    example_data %>%
     add_count(group) %>%
     slice(which(dense_rank(desc(n)) <= 2)) %>%
     select(-n)
    
        4
  •  1
  •   OTStats Redfood    6 年前

    另一个 dplyr 方法可以是:

    example_data %>% 
      group_by(group) %>% 
      count() %>% 
      ungroup() %>% 
      top_n(n = 2, wt = n) %>% 
      select(-n) %>% 
      inner_join(example_data)
    
        5
  •  0
  •   utubun    6 年前

    另一个解决方案(类似于@otstats):

    library(dplyr)
    
    example_data %>% 
      inner_join(
        count(., group) %>% top_n(2, n) %>% dplyr::select(group)
        )
    
    #    col1 col2 group
    # 1     1   16     2
    # 2     3   18     3
    # 3     4   19     2
    # 4     5   20     3
    # 5     7   22     3
    # 6     9   24     3
    # 7    11   26     2
    # 8    12   27     2
    # 9    13   28     2
    # 10   14   29     3
    # 11   15   30     3