减去数据帧的操作 [英] minus operation of data frames

查看:136
本文介绍了减去数据帧的操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个数据框 df1 df2

df1 <- data.frame(c1=c("a","b","c","d"),c2=c(1,2,3,4) )
df2 <- data.frame(c1=c("c","d","e","f"),c2=c(3,4,5,6) )
> df1
  c1 c2
1  a  1
2  b  2
3  c  3
4  d  4
> df2
  c1 c2
1  c  3
2  d  4
3  e  5
4  f  6

我需要执行这2个数据帧的设置操作。我使用 merge(df1,df2,all = TRUE) merge(df1,df2,all = FALSE)得到这些数据帧的联合和交集,并得到所需的输出。获取这些数据帧的减号的功能是什么,即一个数据帧上存在的所有位置,而不是另一个数据帧的位置?我需要以下输出。

I need to perform set operation of these 2 data frames. I used merge(df1,df2,all=TRUE) and merge(df1,df2,all=FALSE) method to get the union and intersection of these data frames and got the required output. What is the function to get the minus of these data frames,that is all the positions existing on one data frame but not the other? I need the following output.

 c1 c2
1  a  1
2  b  2


推荐答案

我记得遇到这个几个月后。管理筛选我的Evernote一线

I remember coming across this exact issue quite a few months back. Managed to sift through my Evernote one-liners.

注意:这是不是我的解决方案。信用于谁写的(我现在似乎找不到)。

Note: This is not my solution. Credit goes to whoever wrote it (whom I can't seem to find at the moment).

如果您不用担心 rownames ,那么您可以执行以下操作:

If you don't worry about rownames then you can do:

df1[!duplicated(rbind(df2, df1))[-seq_len(nrow(df2))], ]
#   c1 c2
# 1  a  1
# 2  b  2






编辑 data.table 解决方案:

dt1 <- data.table(df1, key="c1")
dt2 <- data.table(df2)
dt1[!dt2]

或更好的one-liner(从v1.9.6 +):

or better one-liner (from v1.9.6+):

setDT(df1)[!df2, on="c1"]

这将返回 df1 中的所有行,其中 df2 $ c1 df1 $ c1

This returns all rows in df1 where df2$c1 doesn't have a match with df1$c1.

这篇关于减去数据帧的操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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