Subsetting based on values of a different data frame in R

后端 未结 4 976
一向
一向 2021-01-19 04:59

I want to subset data if every value in the row is greater than the respective row in a different data frame. I also need to skip some top rows. These previous questions did

相关标签:
4条回答
  • 2021-01-19 05:01

    I think it is better to use SQL for such inter table filtering. It is clean and readable( You keep the rules logic).

     library(sqldf)
    sqldf('SELECT DISTINCT A.*
            FROM A,B
            WHERE A.name1   > B.name1
            AND    A.name2  > B.name2')
      name1 name2
    1   trt  ctrl
    2    10    10
    
    0 讨论(0)
  • 2021-01-19 05:05

    requisite data.table solution:

    library(data.table)
    
    # just to preserve the order, non-alphabetically
    idsA <- factor(rownames(A), levels=rownames(A))
    idsB <- factor(rownames(B), levels=rownames(B))
    
    # convert to data.table with id
    ADT <- data.table(id=idsA, A, key="id")
    BDT <- data.table(id=idsB, B, key="id")
    
    # filter as needed
    ADT[BDT][name1 > name1.1 & name2 > name2.1, list(id, name1, name2)]
    
    0 讨论(0)
  • 2021-01-19 05:08

    If I rename your matrices amat and bmat, then

    amat[which(sapply(1:nrows(amat),function(x) prod(amat[x,]>bmat[x,]))==1),]
    [1] 10 10
    

    And you can paste the 'hours' row back on if desired.

    0 讨论(0)
  • 2021-01-19 05:23
    N <- nrow(A)
    cond <- sapply(3:N, function(i) sum(A[i,] > B[i,])==2)
    rbind(A[1:2,], subset(A[3:N,], cond))
    
    0 讨论(0)
提交回复
热议问题