R filtering out a subset

前端 未结 6 1356
误落风尘
误落风尘 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:29

    This is not the fastest and is likely to be very slow but is an alternative to mplourde's that takes into account the row data and should work on mixed data which flodel critiqued. It relies on the paste2 function from the qdap package which doesn't exist yet as I plan to release it within the enxt month or 2:

    Paste 2 function:

    paste2 <- function(multi.columns, sep=".", handle.na=TRUE, trim=TRUE){
    
        if (trim) multi.columns <- lapply(multi.columns, function(x) {
                gsub("^\\s+|\\s+$", "", x)
            }
        )
    
        if (!is.data.frame(multi.columns) & is.list(multi.columns)) {
            multi.columns <- do.call('cbind', multi.columns)
          }
    
        m <- if(handle.na){
                     apply(multi.columns, 1, function(x){if(any(is.na(x))){
                           NA
                     } else {
                           paste(x, collapse = sep)
                     }
                 }
             )   
             } else {
              apply(multi.columns, 1, paste, collapse = sep)
        }
        names(m) <- NULL
        return(m)
    }
    

    # Flodel's mixed data set:

    A <- data.frame(x = 1:4, y = as.character(1:4)); B <- A[1:2, ]
    

    # My approach:

    A[!paste2(A)%in%paste2(B), ]
    

提交回复
热议问题