根据另一个数据框中的列删除行 [英] Removing rows based on column in another dataframe

查看:119
本文介绍了根据另一个数据框中的列删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据另一个数据框的列,有没有办法从数据帧中删除行?

Is there a way to remove rows from a dataframe, based on the column of another dataframe?

例如,Dataframe 1:

For example, Dataframe 1:

        Gene CHROM    POS REF ALT N_INFORMATIVE     Test       Beta       SE
AAA     1  15211   T   G          1481  1:15211 -0.0599805 0.112445
LLL     1 762061   T   A          1481 1:762061  0.2144100 0.427085
CCC     1 762109   C   T          1481 1:762109  0.2847510 0.204255
DDD     1 762273   G   A          1481 1:762273  0.0443946 0.119924

数据帧2(仅1列):

Genes
AAA
BBB
CCC
DDD
EEE
FFF

在这个框架中,我想扫描Dataframe 1,列1,与Dataframe进行任何匹配2,删除匹配行。

In this situtation, I want to scan Dataframe 1, column 1 for any matches to Dataframe 2, and remove matching rows.

他们需要完全匹配,结果如下所示:

They need to be an exact match, and the result would look like this:

        Gene CHROM    POS REF ALT N_INFORMATIVE     Test       Beta       SE
LLL     1 762061   T   A          1481 1:762061  0.2144100 0.427085

我尝试过这样的变体,但它没有起作用:

I've tried variations of this, but it hasn't worked:

NewDataframe <-!(Dataframe1$Gene==Dataframe2$Genes)

感谢阅读。 $ b

推荐答案

使用%中的%来识别第一个数据框中的哪些元素不包含在第二数据帧,然后将所得到的逻辑向量传递到第一数据帧到子集。

Use %in% to identify which elements from the first data frame are not contained in the second data frame, then pass the resulting logical vector to the first data frame to subset.

dat1 <- data.frame(id = LETTERS[1:10], stringsAsFactors = FALSE)
dat2 <- data.frame(id = c("B", "D"), stringsAsFactors = FALSE)
dat1[!dat1$id %in% dat2$id, , drop = FALSE]
#    id
# 1   A
# 3   C
# 5   E
# 6   F
# 7   G
# 8   H
# 9   I
# 10  J

这篇关于根据另一个数据框中的列删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆