If there is an entry >= 10, I try to set all other values for that ID to zero for that specific year in a data table. All values stay the same if there is no value bigger or equal to 10. If there are more than one value bigger than 10, only the biggest entry should be kept.
Here I have a sample data table:
library(data.table)
data = data.table(
ID = c("a1", "a2", "a2", "a1", "a2", "a1", "a1"),
"2018" = c(3,5,11,3,9,22,6),
"2019" = c(3,5,6,21,1,4,0),
"2020" = c(0,4,13,9,16,7,9),
"2021" = c(4,0,3,8,5,4,6))
I tried it with a for loop, but I wasn't able to do it with two dimensions. The desired outcome would look like that:
solution <- data.table(
ID = c("a1", "a2", "a2", "a1", "a2", "a1", "a1"),
"2018" = c(0,0,11,0,0,22,0),
"2019" = c(0,5,6,21,1,0,0),
"2020" = c(0,0,0,9,16,7,9),
"2021" = c(4,0,3,8,5,4,6))