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:)