gpt4 book ai didi

r - 收集 R 中相同组内的重叠坐标列

转载 作者:行者123 更新时间:2023-12-05 03:26:45 26 4
gpt4 key购买 nike

我有一个数据框,例如

    Seq Chrm  start  end  length  score
0 A C1 1 50 49 12
1 B C1 3 55 52 12
2 C C1 6 60 54 12
3 Cbis C1 6 60 54 11
4 D C1 70 120 50 12
5 E C1 78 111 33 12
6 F C2 350 400 50 12
7 A C2 349 400 51 12
8 B C2 450 500 50 12

我想在每个特定的 Chrm 中,在每个重叠的 startend 中保留最长 的行长度 值和最高Score 值。

例如在C1中:

Seq    Chrm start end  length score
A C1 1 50 49 12
B C1 3 55 52 12
C C1 6 60 54 12
Cbis C1 6 60 54 11
D C1 70 120 50 12
E C1 78 111 33 12

A,B,C,Cbisstartend的坐标一起重叠,D和< strong>E 一起重叠。

A,B,C,Cbis组中最长的是CCbis54,那么我保留得分最高的是**C** (12) 在**D,E**组中,最长的是**D**,50`。所以我只保留 CD 行。

如果我对其他 Chrm 执行相同的操作,我应该会得到以下输出:

Seq Chrm start end  length score
C C1 6 60 54 12
D C1 70 120 50 12
A C2 349 400 51 12
B C2 450 500 50 12

如果有帮助,这里是 dput 格式的数据帧:

structure(list(Seq = c("A", "B", "C", "Cbis", "D", "E", "F", 
"A", "B"), Chrm = c("C1", "C1", "C1", "C1", "C1", "C1", "C2",
"C2", "C2"), start = c(1L, 3L, 6L, 6L, 70L, 78L, 350L, 349L,
450L), end = c(50L, 55L, 60L, 60L, 120L, 111L, 400L, 400L, 500L
), length = c(49L, 52L, 54L, 54L, 50L, 33L, 50L, 51L, 50L), score = c(12L,
12L, 12L, 11L, 12L, 12L, 12L, 12L, 12L)), class = "data.frame", row.names = c(NA,
-9L))

最佳答案

使用 tidyverse 函数:

library(tidyverse)

dat %>%
group_by(Chrm) %>%
arrange(start, end) %>%
group_by(cum = head(c(0, cumsum((end < lead(start)) | (end > lead(start) & start > lead(end)))), -1)) %>%
arrange(desc(length, score)) %>%
slice_head(n = 1)

Seq Chrm start end length score cum
<chr> <chr> <int> <int> <int> <int> <dbl>
1 C C1 6 60 54 12 0
2 D C1 70 120 50 12 1
3 A C2 349 400 51 12 2
4 B C2 450 500 50 12 3

关于r - 收集 R 中相同组内的重叠坐标列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71661933/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com