使用R删除反向重复项 [英] Deleting reversed duplicates with R
问题描述
我在 R 中有一个数据框,其中包含拟南芥中旁系同源基因的基因ID,看起来像这样:
I have a data frame in R that contains the gene ids of paralogous genes in Arabidopsis, looking something like this:
gene_x gene_y
AT1 AT2
AT3 AT4
AT1 AT2
AT1 AT3
AT2 AT1
'ATx'与基因名称相对应。
with the 'ATx' corresponding to the gene names.
现在,用于下游分析,我只想继续使用唯一的一对。有些对只是简单的重复项,可以使用 duplicated()
函数轻松删除。
但是,上面的人工数据框中的第五行也是重复的,但是顺序相反, duplicated()
不会将其提取。 ,也不是通过 unique()
函数实现的。
Now, for downstream analysis, I would want to continue only with the unique pairs. Some pairs are just simple duplicates and can be removed easily upon using the duplicated()
function.
However, the fifth row in the artificial data frame above is also a duplicate, but in reversed order, and which will not be picked up by the duplicated()
, nor by the unique()
function.
关于如何删除这些行的任何想法?
Any ideas in how to remove these rows?
推荐答案
mydf <- read.table(text="gene_x gene_y
AT1 AT2
AT3 AT4
AT1 AT2
AT1 AT3
AT2 AT1", header=TRUE, stringsAsFactors=FALSE)
这里是使用 apply
, sort
,粘贴
,和重复
:
mydf[!duplicated(apply(mydf,1,function(x) paste(sort(x),collapse=''))),]
gene_x gene_y
1 AT1 AT2
2 AT3 AT4
4 AT1 AT3
这是一个略有不同的解决方案:
And here's a slightly different solution:
mydf[!duplicated(lapply(as.data.frame(t(mydf), stringsAsFactors=FALSE), sort)),]
gene_x gene_y
1 AT1 AT2
2 AT3 AT4
4 AT1 AT3
这篇关于使用R删除反向重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!