Delete duplicates of observations with a value above a certain threshold

Viewed 50

If an observation is bigger than a certain threshold (in the example below >=5), I want to delete the smaller duplicates in a data.table. I tried it with the following:

library(data.table)

data = data.table(
a = c("a1", "a2", "a3", "a4", "a2", "a3", "a1"),
b = c("4", "2", "7", "3", "8", "2", "2"))

data <- data[order(a, b, decreasing = TRUE),]
data <- for (i in 1 : nrow(data)){
    if (data$b[i] >= 5){
        data[!duplicated(data[, c('a')]),]
        }}

With this command, I get an empty data.table. But I would expect a table like that:

data = data.table(
a = c("a4", "a3", "a2", "a1", "a1"),
b = c("3", "7", "8", "4", "2"))

Thanks a lot for you help:)

1 Answers

this should work

bval <- 5 
data[!a %in% data[b >= bval, a] | (a %in% data[b >= bval, a] & b >= bval), ]
    a b
1: a1 4
2: a3 7
3: a4 3
4: a2 8
5: a1 2

If filters data using the following logic:
keep rows if
!a %in% data[b >= bval, a] a has no b-values equal to (or greater than) 5
or |
(a %in% data[b >= bval, a] & b >= bval) a has b-values equal to (or greater than 5) and & the value of b is equal to (or greater than) 5.

Related