R语言 根据一对中的哪一行较高来分配值

f45qwnt8  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(87)

我有:

> dput(for_stack)
structure(list(id = c("20230420-01", "20230420-02", "2023042110-01", 
"2023042110-02", "2023042112-01", "2023042112-02", "2023042114-01", 
"2023042114-02", "2023042214-01", "2023042214-02"), pair_id = c(20230420L, 
20230420L, 2023042110L, 2023042110L, 2023042112L, 2023042112L, 
2023042114L, 2023042114L, 2023042214L, 2023042214L), mean_blast = c(3.82352941176471, 
4.46153846153846, 1.71428571428571, 1.0625, 4.8125, 4, 3.5, 1.25, 
4.9375, 4.5)), row.names = c(NA, 10L), class = "data.frame")

字符串
每个pair_id有两行,我想知道每对中哪一行包含更高的mean_blast值,使得:

> dput(for_stack)
structure(list(id = c("20230420-01", "20230420-02", "2023042110-01", 
"2023042110-02", "2023042112-01", "2023042112-02", "2023042114-01", 
"2023042114-02", "2023042214-01", "2023042214-02"), pair_id = c(20230420L, 
20230420L, 2023042110L, 2023042110L, 2023042112L, 2023042112L, 
2023042114L, 2023042114L, 2023042214L, 2023042214L), mean_blast = c(3.82352941176471, 
4.46153846153846, 1.71428571428571, 1.0625, 4.8125, 4, 3.5, 1.25, 
4.9375, 4.5), higher = c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, 
TRUE, FALSE, TRUE, FALSE)), row.names = c(NA, 10L), class = "data.frame")

              id    pair_id mean_blast higher
1    20230420-01   20230420   3.823529  FALSE
2    20230420-02   20230420   4.461538   TRUE
3  2023042110-01 2023042110   1.714286   TRUE
4  2023042110-02 2023042110   1.062500  FALSE
5  2023042112-01 2023042112   4.812500   TRUE
6  2023042112-02 2023042112   4.000000  FALSE
7  2023042114-01 2023042114   3.500000   TRUE
8  2023042114-02 2023042114   1.250000  FALSE
9  2023042214-01 2023042214   4.937500   TRUE
10 2023042214-02 2023042214   4.500000  FALSE


我相信有一种方法可以用summarizegroup_by来实现这一点,但我还没有弄清楚。

vsdwdz23

vsdwdz231#

dplyr中,您可以按组使用max

library(dplyr)

for_stack |>
  mutate(higher = mean_blast == max(mean_blast), .by = pair_id)

字符串

wyyhbhjk

wyyhbhjk2#

在基数R中,你可以在ave中使用rank2是更高的值。

> transform(for_stack, higher=ave(mean_blast, pair_id, FUN=rank) == 2)
              id    pair_id mean_blast higher
1    20230420-01   20230420   3.823529  FALSE
2    20230420-02   20230420   4.461538   TRUE
3  2023042110-01 2023042110   1.714286   TRUE
4  2023042110-02 2023042110   1.062500  FALSE
5  2023042112-01 2023042112   4.812500   TRUE
6  2023042112-02 2023042112   4.000000  FALSE
7  2023042114-01 2023042114   3.500000   TRUE
8  2023042114-02 2023042114   1.250000  FALSE
9  2023042214-01 2023042214   4.937500   TRUE
10 2023042214-02 2023042214   4.500000  FALSE

字符串
如果有平局,两者都得到FALSE

相关问题