简体   繁体   English

R:过滤器(函数句柄,数据)在 R 中是什么意思

[英]R: what does Filter(function handle, data) means in R

The variable getgenes has a column named "GENE".变量 getgenes 有一个名为“GENE”的列。 This column has redundant strings.此列有多余的字符串。 The purpose is to get the strings that appeared g(g can be 2,3,4...etc) times.目的是获取出现 g(g 可以是 2,3,4...等) 次的字符串。

x <- Filter(function(elem) length(which(getgenes$GENE == elem)) == g, getgenes$GENE)

I searched Filter function in R but cannot find a match that use functon as 1st argument.我在 R 中搜索了过滤器 function 但找不到使用函数作为第一个参数的匹配项。

What does the Filter means here?过滤器在这里是什么意思?

From the help page, "Filter extracts the elements of a vector for which a predicate (logical) function gives true."从帮助页面中,“过滤器提取谓词(逻辑)function 为真的向量的元素。” If you look at the source, you can see that如果你查看源代码,你可以看到

x <- Filter(function(elem) length(which(getgenes$GENE == elem)) == g, getgenes$GENE)

is equivalent to:相当于:

x <- getgenes$GENE[which(as.logical(unlist(lapply(getgenes$GENE,
                                                  function(elem) length(which(getgenes$GENE == elem)) == g))))]

However, this seems a roundabout way of arriving at the answer.然而,这似乎是一种迂回的方式来得出答案。 Under some mild assumptions about the structure of getgenes and g, I believe the expression can be simplified to:在一些关于getgenes和g的结构的温和假设下,我相信表达式可以简化为:

x <- getgenes$GENE[getgenes$GENE %in% names(which(table(getgenes$GENE) == g))]

if you are willing to forgo duplicates, it can be simplified even further.如果您愿意放弃重复,则可以进一步简化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM