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
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
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)]
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.
N <- nrow(A)
cond <- sapply(3:N, function(i) sum(A[i,] > B[i,])==2)
rbind(A[1:2,], subset(A[3:N,], cond))