Minus operation of data frames

前端 未结 7 764
忘掉有多难
忘掉有多难 2020-12-05 18:53

I have 2 data frames df1 and df2.

df1 <- data.frame(c1=c(\"a\",\"b\",\"c\",\"d\"),c2=c(1,2,3,4) )
df2 <- data.frame(c1=c(\"         


        
7条回答
  •  天涯浪人
    2020-12-05 19:11

    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).

    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
    

    Edit: A data.table solution:

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

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

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

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

提交回复
热议问题