子集一个表,通过两个列在R中的查找表中的任何参考范围内

发布于 2025-02-08 21:49:09 字数 646 浏览 3 评论 0原文

R TABLE1:实际上还有更多行

PatientID chr pos  type end  length 
AB1       1  2431  DEL 2100  -331
AC3       1  98041 INV 99100  1059
AG6       1  8743  BND 9000   257

表2:实际上,更多的行,第2和3列是外显子所需输出的范围

Exon 2001 2500 
Exon 8700 8750 

:从表1中的那些行,其中变体的任何部分(来自pos-end)(来自pos-end)属于任何地方在Table2输出中的外显子的范围中

PatientID chr pos  type end   length 
AB1       1  2431  DEL 2100  -331
AG6       1  8743  BND 9000   257

我尝试了一个子集:

subset(table1$pos >= table2$V2 | table1$end <= table$end) 

但这并不能给我所需的输出。任何帮助都将不胜感激。

一切顺利

R Table1: In reality many more rows

PatientID chr pos  type end  length 
AB1       1  2431  DEL 2100  -331
AC3       1  98041 INV 99100  1059
AG6       1  8743  BND 9000   257

Table 2: In reality many more rows, columns 2 and 3 are the ranges for the exon

Exon 2001 2500 
Exon 8700 8750 

Desired output: Those rows from table 1 where any part of the variants (from pos-end) fall within anywhere of the range of an Exon in table2

output:

PatientID chr pos  type end   length 
AB1       1  2431  DEL 2100  -331
AG6       1  8743  BND 9000   257

I have tried a subset:

subset(table1$pos >= table2$V2 | table1$end <= table$end) 

but this does not give me my desired output. Any help would be much appreciated.

All the best

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2025-02-15 21:49:10

如果我正确理解您,您想找到的变体(对于Table2至少一个范围, r )至少满足以下条件之一:

  1. table1 $ end end is在 r
  2. 表1 $ pos r
  3. table1 中下面的 r 的相对侧是对

所有可能性的示意图描述,这些可能性是由括号界定的 r ,以及由连字符代表的变体以及符合条件的数量。

-[-  ]   1.
 [  -]-  2.
 [ - ]   1. and 2.
-[---]-  3.

请注意,下面的代码假定(与Table1)Table2 [,2]&lt; = table2 [,3]

# simplified table1
table1 <- data.frame(pos=c(2431, 98041, 8743), end=c(2100, 99100, 9000))
table2 <- data.frame(exon='Exon', from=c(2001, 8700), to=c(2500, 8750))

is.within <- apply(table1[, c('pos', 'end')], 1, function(x) {
  
  x <- sort(x)  # make sure x[1] <= x[2], i.e. pos <= end
  any((x[1] >= table2[, 2] & x[1] <= table2[, 3]) |      # 1.
        (x[2] >= table2[, 2] & x[2] <= table2[, 3]) |    # 2.
        (x[1] <= table2[, 2] & x[2] >= table2[, 3]))     # 3.
        
})
table1[is.within, ]

If I understand you correctly, you want to find variants which (for at least one range in table2, r) meet at least one of the following conditions:

  1. table1$end is within r
  2. table1$pos is within r
  3. table1$pos and table2$end are on opposite sides of r

Below are schematic depictions of all possibilities with r delimited by brackets and a variant represented by hyphens together with the numbers of conditions met.

-[-  ]   1.
 [  -]-  2.
 [ - ]   1. and 2.
-[---]-  3.

Please note that the code below assumes that (unlike table1) table2[, 2] <= table2[, 3].

# simplified table1
table1 <- data.frame(pos=c(2431, 98041, 8743), end=c(2100, 99100, 9000))
table2 <- data.frame(exon='Exon', from=c(2001, 8700), to=c(2500, 8750))

is.within <- apply(table1[, c('pos', 'end')], 1, function(x) {
  
  x <- sort(x)  # make sure x[1] <= x[2], i.e. pos <= end
  any((x[1] >= table2[, 2] & x[1] <= table2[, 3]) |      # 1.
        (x[2] >= table2[, 2] & x[2] <= table2[, 3]) |    # 2.
        (x[1] <= table2[, 2] & x[2] >= table2[, 3]))     # 3.
        
})
table1[is.within, ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文