Minus operation of data frames

前端 未结 7 723
忘掉有多难
忘掉有多难 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:02

    You can create identifier columnas then subset:

    e.g.

    df1 <- data.frame(c1=c("a","b","c","d"),c2=c(1,2,3,4), indf1 = rep("Y",4) )
    df2 <- data.frame(c1=c("c","d","e","f"),c2=c(3,4,5,6),indf2 = rep("Y",4) )
    merge(df1,df2)
    #  c1 c2 indf1 indf2
    #1  c  3     Y     Y
    #2  d  4     Y     Y
    
    bigdf <- merge(df1,df2,all=TRUE)
    #  c1 c2 indf1 indf2
    #1  a  1     Y  
    #2  b  2     Y  
    #3  c  3     Y     Y
    #4  d  4     Y     Y
    #5  e  5       Y
    #6  f  6       Y
    

    Then subset how you wish:

     bigdf[is.na(bigdf$indf1) ,]
    #  c1 c2 indf1 indf2
    #5  e  5       Y
    #6  f  6       Y
    
     bigdf[is.na(bigdf$indf2) ,]  #<- output you requested those not in df2
    #  c1 c2 indf1 indf2
    #1  a  1     Y  
    #2  b  2     Y  
    

提交回复
热议问题