I'm looking for a fast way to remove all dominated rows from a table (preferably using parallel processing, to take advantage of multiple cores).
By "dominated row", I mean a row that is less than or equal to another row in all columns. For example, in the following table:
tribble(~a, ~b, ~c,
10, 5, 3,
10, 4, 2,
1, 4, 1,
7, 3, 6)
Rows 2 and 3 are dominated rows (in this case they are both dominated by row 1), and should be removed. Rows 1 and 4 are not dominated by any other row and should be preserved, resulting in this table:
tribble(~a, ~b, ~c,
10, 5, 3,
7, 3, 6)
To further illustrate, here is the kind of code I'm looking to speed up:
table1 = as_tibble(replicate(3, runif(500000)))
colnames(table1) = c("a", "b", "c")
table2 = table1
for (i in 1:nrow(table1)) {
table2 = filter(table2,
(a > table1[i,]$a | b > table1[i,]$b | c > table1[i,]$c) |
(a == table1[i,]$a & b == table1[i,]$b & c == table1[i,]$c) )
}
filtered_table = table2
I have some ideas, but figured I'd ask if there might be well-known packages/functions that do this.
UPDATE: Here is a fairly simple parallelization of the above code, which nevertheless provides a solid performance boost:
remove_dominated = function(table) {
ncores = detectCores()
registerDoParallel(makeCluster(ncores))
# Divide the table into parts and remove dominated rows from each part
tfref = foreach(part=splitIndices(nrow(table), ncores), .combine=rbind) %dopar% {
tpref = table[part[[1]]:part[[length(part)]],]
tp = tpref
for (i in 1:nrow(tpref)) {
tp = filter(tp,
(a > tpref[i,]$a | b > tpref[i,]$b | c > tpref[i,]$c |
(a == tpref[i,]$b & b == tpref[i,]$b & c == tpref[i,]$c) )
}
tp
}
# After the simplified parts have been concatenated, run a final pass to remove dominated rows from the full table
t = tfref
for (i in 1:nrow(tfref)) {
t = filter(t,
(a > tfref[i,]$a | b > tfref[i,]$b | c > tfref[i,]$c |
(a == tfref[i,]$a & b == tfref[i,]$b & c == tfref[i,]$c) )
}
return(t)
}