删除存在于另一个数据帧中的行? [英] Delete rows that exist in another data frame?

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

问题描述

我有以下两个数据框(示例):



df1:

 code> name profile type strand 
A 4.5 1 +
B 3.2 1 +
C 5.5 1 +
D 14.0 1 -
E 45.1 1 -
F 32.8 1 -
G 19.9 1 +

df2:

 名称
A
B
C
G

我想删除 df1 df1 $ name = df2 $ name 以获取以下内容:



输出:

  name profile type strand 
D 14.0 1 -
E 45.1 1 -
F 32.8 1 -
/ pre>

如果有人可以告诉我使用哪一段代码会有很多帮助,起初似乎很简单,但是自从昨天起就一直在搞乱。

解决方案

您需要%运算符中的%。因此,

  df1 [!(df1 $ name%in%df2 $ name),] 

应该给你想要的。




  • df1 $ name%in%df2 $ name 测试 df1 $ name 中的值是否在 df2 $ name

  • 运算符反转结果。


I have the two following data frames (example):

df1:

name    profile    type    strand
A       4.5        1       +
B       3.2        1       +
C       5.5        1       +
D       14.0       1       -
E       45.1       1       -
F       32.8       1       -
G       19.9       1       +

df2:

name
A
B
C
G

I would like to delete the rows in df1 for which df1$name = df2$name to get the following:

Output:

name    profile    type    strand
D       14.0       1       -
E       45.1       1       -
F       32.8       1       -

If anyone could tell me which piece of code to use it would be a lot of help, seemed simple at first but I've been messing it up since yesterday.

解决方案

You need the %in% operator. So,

df1[!(df1$name %in% df2$name),]

should give you what you want.

  • df1$name %in% df2$name tests whether the values in df1$name are in df2$name
  • The ! operator reverses the result.

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

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