我有一个表,每个样本都有一个唯一的标识符,但也有一个部分标识符。我想提取每个部分的所有与所有距离比较(此数据来自第二个表)
例如表 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] - 即表二中共享一个部分的样本的所有距离比较表格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
然后,我们可以过滤table2
通过table1_cross
。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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句