R 中的值区间 windows
Value intervals windows in R
我有几个 windows 区间看起来像这样:
Start Stop
19136293 19138512
20708651 20716555
31063462 31064384
然后我有这样的数据:
Read TrueC MapC TruePos MappedPos ResMap
JLN6 22 22 16050005 16050091 TP
9MRW 22 22 16050032 16050032 TP
GRR3 22 14 16050075 19792677 FP
V19X 22 * 17023341 0 FN
而且我正在尝试查找前一个windows内外的阅读量;如果 TruePos 值在一个 window 间隔内,则读取在 window 内。我的目标是 TP/FP/FN(ResMap 列)内外的数量 windows.
我想在我的 windows 文件上创建 for 循环,然后逐行检查我的数据并对其求和,但我有 6 亿行,我确信有一种最快的方法..就是不知道。
非常感谢,
干杯,
卡加米
由于您没有提供任何可重现的数据,我使用了一些自己的样本数据
dput(range)
structure(list(start = c(10L, 25L, 50L, 61L, 85L, 100L), end = c(15L,
27L, 53L, 66L, 89L, 102L)), class = "data.frame", row.names = c(NA,
-6L))
> range
start end
1 10 15
2 25 27
3 50 53
4 61 66
5 85 89
6 100 102
> dput(df)
structure(list(Id = 1:15, truepos = c(65L, 59L, 61L, 74L, 92L,
49L, 72L, 96L, 81L, 2L, 34L, 27L, 66L, 87L, 19L)), class = "data.frame", row.names = c(NA,
-15L))
> df
Id truepos
1 1 65
2 2 59
3 3 61
4 4 74
5 5 92
6 6 49
7 7 72
8 8 96
9 9 81
10 10 2
11 11 34
12 12 27
13 13 66
14 14 87
15 15 19
我已经使用名为 fuzzyjoin
的程序包来执行此操作,只要 TRUEPOS
在其中一个中,它就会添加一个新列 inside_range
并将值设为 True
范围
library(fuzzyjoin)
fuzzy_left_join(df, range, by = c("truepos" = "start", "truepos" = "end"),
match_fun = list(`>=`, `<`)) %>% mutate(inside_range = !is.na(start)) %>%
select(-start, -end)
> Id truepos inside_range
1 1 65 TRUE
2 2 59 FALSE
3 3 61 TRUE
4 4 74 FALSE
5 5 92 FALSE
6 6 49 FALSE
7 7 72 FALSE
8 8 96 FALSE
9 9 81 FALSE
10 10 2 FALSE
11 11 34 FALSE
12 12 27 FALSE
13 13 66 FALSE
14 14 87 TRUE
15 15 19 FALSE
我认为这符合您的目的,您可以在您的数据上复制这种方法 df
。祝你好运
我有几个 windows 区间看起来像这样:
Start Stop
19136293 19138512
20708651 20716555
31063462 31064384
然后我有这样的数据:
Read TrueC MapC TruePos MappedPos ResMap
JLN6 22 22 16050005 16050091 TP
9MRW 22 22 16050032 16050032 TP
GRR3 22 14 16050075 19792677 FP
V19X 22 * 17023341 0 FN
而且我正在尝试查找前一个windows内外的阅读量;如果 TruePos 值在一个 window 间隔内,则读取在 window 内。我的目标是 TP/FP/FN(ResMap 列)内外的数量 windows.
我想在我的 windows 文件上创建 for 循环,然后逐行检查我的数据并对其求和,但我有 6 亿行,我确信有一种最快的方法..就是不知道。
非常感谢,
干杯,
卡加米
由于您没有提供任何可重现的数据,我使用了一些自己的样本数据
dput(range)
structure(list(start = c(10L, 25L, 50L, 61L, 85L, 100L), end = c(15L,
27L, 53L, 66L, 89L, 102L)), class = "data.frame", row.names = c(NA,
-6L))
> range
start end
1 10 15
2 25 27
3 50 53
4 61 66
5 85 89
6 100 102
> dput(df)
structure(list(Id = 1:15, truepos = c(65L, 59L, 61L, 74L, 92L,
49L, 72L, 96L, 81L, 2L, 34L, 27L, 66L, 87L, 19L)), class = "data.frame", row.names = c(NA,
-15L))
> df
Id truepos
1 1 65
2 2 59
3 3 61
4 4 74
5 5 92
6 6 49
7 7 72
8 8 96
9 9 81
10 10 2
11 11 34
12 12 27
13 13 66
14 14 87
15 15 19
我已经使用名为 fuzzyjoin
的程序包来执行此操作,只要 TRUEPOS
在其中一个中,它就会添加一个新列 inside_range
并将值设为 True
范围
library(fuzzyjoin)
fuzzy_left_join(df, range, by = c("truepos" = "start", "truepos" = "end"),
match_fun = list(`>=`, `<`)) %>% mutate(inside_range = !is.na(start)) %>%
select(-start, -end)
> Id truepos inside_range
1 1 65 TRUE
2 2 59 FALSE
3 3 61 TRUE
4 4 74 FALSE
5 5 92 FALSE
6 6 49 FALSE
7 7 72 FALSE
8 8 96 FALSE
9 9 81 FALSE
10 10 2 FALSE
11 11 34 FALSE
12 12 27 FALSE
13 13 66 FALSE
14 14 87 TRUE
15 15 19 FALSE
我认为这符合您的目的,您可以在您的数据上复制这种方法 df
。祝你好运