【问题标题】:how to filter out rows with max values of only some of the groups using tidyverse如何使用 tidyverse 过滤掉只有部分组的最大值的行
【发布时间】:2023-02-04 05:46:50
【问题描述】:

我有一个包含两列感兴趣的数据框,如下所示:

V1   V2
1    10
1    56
1    72
1    37
2    59
2    29
2    105
2    93    
3    53
3    40
3    84
3     3
4    62
4    34
4    18
4    42
5    38
5    92
5    79
5    25

我想使用 tidyverse 删除包含 V2 中每个组 V1 = 2 和 V1 = 5 的相应最大值的行。

因此,对于上面的数据框,我想得到输出:

V1   V2
1    10
1    56
1    72
1    37
2    59
2    29
2    93
3    53
3    40
3    84
3     3
4    62
4    34
4    18
4    42
5    38
5    79
5    25

这是制作数据框的代码:

df = data.frame(V1= rep(1:5, each=4), V2 = sample(1:100,20))

【问题讨论】:

    标签: r dataframe dplyr tidyverse


    【解决方案1】:

    做一个filter的群

    df %>% group_by(V1) %>% filter(V2 != max(V2)) %>% ungroup
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-27
      • 2022-06-11
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 2022-11-02
      • 2020-03-27
      • 1970-01-01
      相关资源
      最近更新 更多