This will convert your A values to NA
(not available):
dt$A[-1] <- ifelse(dt$A[-1] / head(dt$A[-1], -1) > 2, NA, dt$A[-1])
Now you can decide what to do with thoses NA
s in your column A, e.g. delete the rows:
dt <- dt[!is.na(dt$A), ]
This will also work for all columns like this:
dt[, -1] <- sapply(dt[, -1], function(x) {
x[-1] <- ifelse(x[-1] / head(x[-1], -1) > 2, NA, x[-1])
x
})
dt <- na.omit(dt) # remove NA rows
And if you want to delete the columns with NA
, you can do it like this:
dt[, c(1, which(!is.na(colSums(dt[, -1]))))]