一列的组合之间的公共列 [英] common column between combination of one column
本文介绍了一列的组合之间的公共列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有来自分析的数据集。为了解释结果,我正在尝试构建数据帧
结果应如下:
基因名称|Motif_id_1|Motif_id_2|发生|匹配序列
此处某些Motif_id可能共享gene_name,且结果应该是Motif_id的两个组合(允许重叠)。
我尝试了以下代码,但结果未给出Motif_id内的组合。
merge_practice <- reshape2::dcast(group_geneid_CT,
motif_id+ motif_id~gene_name,
value.var ="matched_sequence",
drop = T,fill = 0,
fun.aggregate = length )
如果可能,我希望使其具有内存和时间效率,并减少对包的依赖。有人能给我一个不同的观点吗?
推荐答案
library(tidyverse)
data <- tribble(
~gene_name, ~motif_id, ~matched_sequence,
"A", "y1", "ccc",
"A", "y2", "ccc",
"A", "y1", "aaa",
"A", "y2", "aaa",
"A", "y2", "aat",
)
data %>%
pull(motif_id) %>%
unique() %>%
combn(2) %>%
t() %>%
as_tibble() %>%
rename(from = V1, to = V2) %>%
mutate(
co_occurrence = list(from, to) %>% pmap(~ {
bind_rows(
data %>% filter(motif_id == .x) %>% select(-motif_id),
data %>% filter(motif_id == .y) %>% select(-motif_id)
) %>%
count(gene_name, matched_sequence, name = "co_occurrent")
})
) %>%
unnest(co_occurrence)
#> Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
#> Using compatibility `.name_repair`.
#> # A tibble: 3 × 5
#> from to gene_name matched_sequence co_occurrent
#> <chr> <chr> <chr> <chr> <int>
#> 1 y1 y2 A aaa 2
#> 2 y1 y2 A aat 1
#> 3 y1 y2 A ccc 2
由reprex package(v2.0.0)创建于2022-03-01
co_occurrent
如果在两个Motif中都找到,则应为2;如果仅在一个Motif中找到,则应为1。
这篇关于一列的组合之间的公共列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文