select 数据框中的多行,其值等于按组的最大值
select multiple rows from a dataframe with a value equal to highest value by group
我有一个如下所示的数据框:
query <- c('a','a','a','b','b','b','c','c','c')
hit <- c(1,2,3,4,5,6,7,8,9)
score <- c(97,97,96,98,97,96,99,99,98)
df <- data.frame(query,hit,score)
df
query hit score
1 a 1 97
2 a 2 97
3 a 3 96
4 b 4 98
5 b 5 97
6 b 6 96
7 c 7 99
8 c 8 99
9 c 9 98
我想对第一列进行分组,select 所有行的分数等于该组的最高分数。我能想到的最接近的方法是像这样使用 top_n:
df %>%
+ group_by(query) %>%
+ top_n(2,score)
小标题:6 x 3
组:查询 [3]
query hit score
<fctr> <dbl> <dbl>
1 a 1 97
2 a 2 97
3 b 4 98
4 b 5 97
5 c 7 99
6 c 8 99
但显然所做的一切就是给我前两个(或我指定的任何内容)。我想要得到的结果看起来更像这样:
query hit score
<fctr> <dbl> <dbl>
1 a 1 97
2 a 2 97
3 b 4 98
5 c 7 99
6 c 8 99
像往常一样,我假设我遗漏了一些非常简单的东西。
在 dplyr 中,只过滤 score == max(score)
:
group_by(df, query) %>%
filter(score == max(score))
# A tibble: 5 x 3
# Groups: query [3]
# query hit score
# <fctr> <dbl> <dbl>
# 1 a 1 97
# 2 a 2 97
# 3 b 4 98
# 4 c 7 99
# 5 c 8 99
你也可以在 base R 中轻松做到这一点,使用 ave()
:
df[with(df, ave(score, query, FUN = max) == score), ]
# query hit score
# 1 a 1 97
# 2 a 2 97
# 4 b 4 98
# 7 c 7 99
# 8 c 8 99
您的语法基本正确,只需在top_n
中指定n = 1
而不是n = 2
。
query <- c('a','a','a','b','b','b','c','c','c')
hit <- c(1,2,3,4,5,6,7,8,9)
score <- c(97,97,96,98,97,96,99,99,98)
df <- data.frame(query,hit,score)
df %>%
group_by(query) %>%
top_n(n = 1, wt = score)
#> # A tibble: 5 x 3
#> # Groups: query [3]
#> query hit score
#> <fctr> <dbl> <dbl>
#> 1 a 1 97
#> 2 a 2 97
#> 3 b 4 98
#> 4 c 7 99
#> 5 c 8 99
使用 top_n
时,如果出现平局,将返回具有该分数的所有观察值。因此,您可以指定您想要 1 个最高分 (n = 1
),然后将返回每个组中具有该分数的所有观察结果。
我有一个如下所示的数据框:
query <- c('a','a','a','b','b','b','c','c','c')
hit <- c(1,2,3,4,5,6,7,8,9)
score <- c(97,97,96,98,97,96,99,99,98)
df <- data.frame(query,hit,score)
df
query hit score
1 a 1 97
2 a 2 97
3 a 3 96
4 b 4 98
5 b 5 97
6 b 6 96
7 c 7 99
8 c 8 99
9 c 9 98
我想对第一列进行分组,select 所有行的分数等于该组的最高分数。我能想到的最接近的方法是像这样使用 top_n:
df %>%
+ group_by(query) %>%
+ top_n(2,score)
小标题:6 x 3
组:查询 [3]
query hit score
<fctr> <dbl> <dbl>
1 a 1 97
2 a 2 97
3 b 4 98
4 b 5 97
5 c 7 99
6 c 8 99
但显然所做的一切就是给我前两个(或我指定的任何内容)。我想要得到的结果看起来更像这样:
query hit score
<fctr> <dbl> <dbl>
1 a 1 97
2 a 2 97
3 b 4 98
5 c 7 99
6 c 8 99
像往常一样,我假设我遗漏了一些非常简单的东西。
在 dplyr 中,只过滤 score == max(score)
:
group_by(df, query) %>%
filter(score == max(score))
# A tibble: 5 x 3
# Groups: query [3]
# query hit score
# <fctr> <dbl> <dbl>
# 1 a 1 97
# 2 a 2 97
# 3 b 4 98
# 4 c 7 99
# 5 c 8 99
你也可以在 base R 中轻松做到这一点,使用 ave()
:
df[with(df, ave(score, query, FUN = max) == score), ]
# query hit score
# 1 a 1 97
# 2 a 2 97
# 4 b 4 98
# 7 c 7 99
# 8 c 8 99
您的语法基本正确,只需在top_n
中指定n = 1
而不是n = 2
。
query <- c('a','a','a','b','b','b','c','c','c')
hit <- c(1,2,3,4,5,6,7,8,9)
score <- c(97,97,96,98,97,96,99,99,98)
df <- data.frame(query,hit,score)
df %>%
group_by(query) %>%
top_n(n = 1, wt = score)
#> # A tibble: 5 x 3
#> # Groups: query [3]
#> query hit score
#> <fctr> <dbl> <dbl>
#> 1 a 1 97
#> 2 a 2 97
#> 3 b 4 98
#> 4 c 7 99
#> 5 c 8 99
使用 top_n
时,如果出现平局,将返回具有该分数的所有观察值。因此,您可以指定您想要 1 个最高分 (n = 1
),然后将返回每个组中具有该分数的所有观察结果。