理解`Reduce`功能 [英] Understand the `Reduce` function
问题描述
我有一个关于 R 中的 Reduce 函数的问题.我阅读了它的文档,但我仍然有点困惑.所以,我有 5 个带有基因名称的向量.例如:
I have a question about the Reduce function in R. I read its documentation, but I am still confused a bit. So, I have 5 vectors with genes name. For example:
v1 <- c("geneA","geneB",""...)
v2 <- c("geneA","geneC",""...)
v3 <- c("geneD","geneE",""...)
v4 <- c("geneA","geneE",""...)
v5 <- c("geneB","geneC",""...)
我想找出至少两个载体中存在哪些基因.有人建议:
And I would like to find out which genes are present in at least two vectors. Some people have suggested:
Reduce(intersect,list(a,b,c,d,e))
如果有人能向我解释这个语句是如何工作的,我将不胜感激,因为我已经看到 Reduce 用于其他场景.
I would greatly appreciate if someone could please explain to me how this statement works, because I have seen Reduce used in other scenarios.
推荐答案
Reduce
接受一个二元函数和一个数据项列表,并以递归方式连续将该函数应用于列表元素.例如:
Reduce
takes a binary function and a list of data items and successively applies the function to the list elements in a recursive fashion. For example:
Reduce(intersect,list(a,b,c))
与
intersect((intersect(a,b),c)
但是,我认为该构造不会在这里帮助您,因为它只会返回那些对所有向量通用的元素.
However, I don't think that construct will help you here as it will only return those elements that are common to all vectors.
要计算基因出现在其中的载体数量,您可以执行以下操作:
To count the number of vectors that a gene appears in you could do the following:
vlist <- list(v1,v2,v3,v4,v5)
addmargins(table(gene=unlist(vlist), vec=rep(paste0("v",1:5),times=sapply(vlist,length))),2,list(Count=function(x) sum(x[x>0])))
vec
gene v1 v2 v3 v4 v5 Count
geneA 1 1 0 1 0 3
geneB 1 0 0 0 1 2
geneC 0 1 0 0 1 2
geneD 0 0 1 0 0 1
geneE 0 0 1 1 0 2
这篇关于理解`Reduce`功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!