[英]filter items from one column based on shared items in another R
我有一个表,每个样本都有一个唯一的标识符,还有一个节标识符。 我想提取每个部分的所有距离对比和所有距离对比(此数据来自第二张表)
例如表1
Sample Section
1 1
2 1
3 1
4 2
5 2
6 3
表2
sample sample distance
1 2 10
1 3 1
1 4 2
2 3 5
2 4 10
3 4 11
因此,我想要的输出是一个列表,该列表的距离为:[1 vs 2],[1 vs 3],[2 vs 3],[4 vs 5]-即表2中所有共享一个截面的样本的距离比较表格1
我开始尝试使用嵌套的for循环来执行此操作,但是很快就变得混乱了。
使用dplyr的解决方案。
我们首先可以创建一个数据框,显示每个部分中样本的组合。
library(dplyr)
table1_cross <- full_join(table1, table1, by = "Section") %>% # Full join by Section
filter(Sample.x != Sample.y) %>% # Remove records with same samples
rowwise() %>%
mutate(Sample.all = toString(sort(c(Sample.x, Sample.y)))) %>% # Create a column showing the combination between Sample.x and Sample.y
ungroup() %>%
distinct(Sample.all, .keep_all = TRUE) %>% # Remove duplicates in Sample.all
select(Sample1 = Sample.x, Sample2 = Sample.y, Section)
table1_cross
# # A tibble: 4 x 3
# Sample1 Sample2 Section
# <int> <int> <int>
# 1 1 2 1
# 2 1 3 1
# 3 2 3 1
# 4 4 5 2
然后,我们可以通过table1_cross
过滤table2
。 table3
是最终输出。
table3 <- table2 %>%
semi_join(table1_cross, by = c("Sample1", "Sample2")) # Filter table2 based on table1_corss
table3
# Sample1 Sample2 distance
# 1 1 2 10
# 2 1 3 1
# 3 2 3 5
数据
table1 <- read.table(text = "Sample Section
1 1
2 1
3 1
4 2
5 2
6 3",
header = TRUE, stringsAsFactors = FALSE)
table2 <- read.table(text = "Sample1 Sample2 distance
1 2 10
1 3 1
1 4 2
2 3 5
2 4 10
3 4 11",
header = TRUE, stringsAsFactors = FALSE)
OP要求找到与table2
共享一个table1
部分的样本的所有table2
距离比较。
这可以通过两种不同的方法来实现:
Sample1
和Sample2
各table1
和保持的只有那些行table2
。其中,部分ID匹配。 table2
table1
每个部分创建所有唯一的样本ID组合,并在table2
找到适当的条目(如果有)。 tmp <- merge(table2, table1, by.x = "Sample1", by.y = "Sample")
tmp <- merge(tmp, table1, by.x = "Sample2", by.y = "Sample")
tmp[tmp$Section.x == tmp$Section.y, c("Sample2", "Sample1", "distance")]
Sample2 Sample1 distance 1 2 1 10 2 3 1 1 3 3 2 5
dplyr
library(dplyr)
table2 %>%
inner_join(table1, by = c(Sample1 = "Sample")) %>%
inner_join(table1, by = c(Sample2 = "Sample")) %>%
filter(Section.x == Section.y) %>%
select(-Section.x, -Section.y)
Sample1 Sample2 distance 1 1 2 10 2 1 3 1 3 2 3 5
data.table
使用嵌套联接
library(data.table)
tmp <- setDT(table1)[setDT(table2), on = .(Sample == Sample1)]
table1[tmp, on = .(Sample == Sample2)][
Section == i.Section, .(Sample1 = i.Sample, Sample2 = Sample, distance)]
使用merge()和链接的data.table表达式
tmp <- merge(setDT(table2), setDT(table1), by.x = "Sample1", by.y = "Sample")
merge(tmp, table1, by.x = "Sample2", by.y = "Sample")[
Section.x == Section.y, -c("Section.x", "Section.y")]
Sample2 Sample1 distance 1: 2 1 10 2: 3 1 1 3: 3 2 5
table1_cross <- do.call(rbind, lst <- lapply(
split(table1, table1$Section),
function(x) as.data.frame(combinat::combn2(x$Sample))))
merge(table2, table1_cross, by.x = c("Sample1", "Sample2"), by.y = c("V1", "V2"))
此处,使用了方便的combn2(x)
函数,该函数生成一次取两个的x元素的所有组合,例如,
combinat::combn2(1:3)
[,1] [,2] [1,] 1 2 [2,] 1 3 [3,] 2 3
繁琐的部分是施加combn2()
到每个组的Section
分开,并创建其可以合并一个data.frame,最后。
dplyr
这是www方法的简化版本
full_join(table1, table1, by = "Section") %>%
filter(Sample.x < Sample.y) %>%
semi_join(x = table2, y = ., by = c(Sample1 = "Sample.x", Sample2 = "Sample.y"))
library(data.table)
setDT(table2)[setDT(table1)[table1, on = .(Section, Sample < Sample), allow = TRUE,
.(Section, Sample1 = x.Sample, Sample2 = i.Sample)],
on = .(Sample1, Sample2), nomatch = 0L]
Sample1 Sample2 distance Section 1: 1 2 10 1 2: 1 3 1 1 3: 2 3 5 1
在这里,非等参联接用于为每个Section
创建Sample
的唯一组合。 这等效于使用combn2()
:
setDT(table1)[table1, on = .(Section, Sample < Sample), allow = TRUE,
.(Section, Sample1 = x.Sample, Sample2 = i.Sample)]
Section Sample1 Sample2 1: 1 NA 1 2: 1 1 2 3: 1 1 3 4: 1 2 3 5: 2 NA 4 6: 2 4 5 7: 3 NA 6
NA
行将在最终连接中删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.