0

我有两个包含基因组数据的数据框,我需要删除数据框 1 中“特征”列中的条目等于数据框 2 行中“特征”列中的条目的所有行。

df1 <- data.frame(feature=c("ENSG419","ENSG1617","ENSG1629","ENSG16230"),distance=c(9833,2460,50538,51162),origin=c("e2","e2","e2","e2"))
df2 <- data.frame(feature=c("ENSG4939","ENSG1617","ENSG5844","ENSG10292"),distance=c(8441,8970,10320,139),origin=c("etoh","etoh","etoh","etoh"))

> df1
    feature distance origin
1   ENSG419     9833     e2
2  ENSG1617     2460     e2
3  ENSG1629    50538     e2
4 ENSG16230    51162     e2
> df2
    feature distance origin
1  ENSG4939     8441   etoh
2  ENSG1617     8970   etoh
3  ENSG5844    10320   etoh
4 ENSG10292      139   etoh

我想得到这个:

    feature distance origin
1   ENSG419     9833     e2
2  ENSG1629    50538     e2
3 ENSG16230    51162     e2

我试图通过将两个数据框绑定到一个新数据框并随后提取具有新数据框相同特征的行来删除重复的条目。现在我想从原始数据框 1 中删除所述行。

df_new <- rbind(df1,df2)
df_new[duplicated(df_new[,1]),]

它不太奏效,无论如何,我相信有更好的解决方案。我会非常感谢任何建议!

4

2 回答 2

3

试试这个:

df1[!df1$feature %in% df2$feature, ]
于 2013-04-04T13:19:09.977 回答
2

我将从两者中提取特征,对集合进行差异化,然后根据结果对第一个数据帧进行子集化。

only1 <- setdiff(df1$feature, df2$feature)
df_sel <- df1[df1$feature %in% only1]

但我同意 Arun 的解决方案是 oneliner :)

于 2013-04-04T13:19:07.900 回答