r - 如果基因存在于一组基因列表的大多数中,如何保留基因?

我有 9 个基因列表,每个基因长度为 2000 个。如果存在于 6 个或更多列表中,我想保留基因。我不知道如何指定这个,我一直在使用 intersect 函数。

任何帮助表示赞赏。

回答1

首先,使用一些辅助函数重新创建数据:

# gene generating function as a trigram of lower case letters
gene <- function(...) {
  paste(sample(letters, 3), collapse = "")
}

# creating lists of genes
gene_lists <- lapply(seq(9), function(x) sapply(seq(2000), gene))

然后提取独特的元素:

# getting unique genes
unique_genes <- unique(unlist(gene_lists))
length(unique_genes)
[1] 10661

在这里我们可以检查合成数据是否有一些冗余:

# checking if there are enough redundant genes
stopifnot(length(unique_genes) < length(unlist(gene_lists)))

然后在计算出现次数的同时迭代唯一基因并列出:

# iterating over unique_genes
gene_occurence <- sapply(unique_genes, function(gene) {
  # iterating over lists
  # sum counts the total number of occurence
    sum(sapply(gene_lists, function(x) { gene %in% x }))
  })
length(gene_occurence)
[1] 10661
table(gene_occurence)
   1    2    3    4    5    6 
6017 3330 1050  231   31    2

然后得到共同基因:

limit <- 6
common_genes <- unique_genes[which(gene_occurence >= limit)]
common_genes
[1] "ngu" "het"

相似文章

scala - 多个列表的组合 (Scala)

我正在尝试编写一个函数,该函数将多个列表作为输入并返回这些列表之间每个组合的字符串表示形式。样本输入:valintegers=List(1,2,3,4)valcharacters=List('a','...

随机推荐

最新文章