R filtering out a subset

前端 未结 6 1346
误落风尘
误落风尘 2021-01-29 13:30

I have a data.frame A and a data.frame B which contains a subset of A

How can I create a data.frame C which is data.frame A with data.frame B excluded? Thanks for your h

6条回答
  •  礼貌的吻别
    2021-01-29 14:31

    A <- data.frame(x = 1:10, y = 1:10)
    #Random subset of A in B
    B <- A[sample(nrow(A),3),]
    #get A that is not in B
    C <- A[-as.integer(rownames(B)),]
    

    Performance test vis-a-vis mplourde's answer:

    library(rbenchmark)
    f1 <- function() A[- as.integer(rownames(B)),]
    f2 <- function() A[! data.frame(t(A)) %in% data.frame(t(B)), ]
    benchmark(f1(), f2(), replications = 10000, 
              columns = c("test", "elapsed", "relative"),
              order = "elapsed"
              )
    
      test elapsed relative
    1 f1()   1.531   1.0000
    2 f2()   8.846   5.7779
    

    Looking at the rownames is approximately 6x faster. Two calls to transpose can get expensive computationally.

提交回复
热议问题