I am looking to create a new data.table that contains all rows with at least one negative value.
Here is a simple reproducible datatable:
dt <- data.table(
ID = c(42, 43, 44),
Stage_1 = c(-6, 7, 4),
Stage_2 = c(-15, 4, -8),
Stage_3 = c(-20, 2, -5)
)
# ID Stage_1 Stage_2 Stage_3
# 1: 42 -6 -15 -20 # <~~ row to be selected (> 0 negative values)
# 2: 43 7 4 2
# 3: 44 4 -8 -5 # <~~ row to be selected (> 0 negative values)
My desired output would be:
dt2 <- data.table(
ID = c(42, 44),
Stage_1 = c(-6, 4),
Stage_2 = c(-15, -8),
Stage_3 = c(-20, -5)
)
# ID Stage_1 Stage_2 Stage_3
# 1: 42 -6 -15 -20
# 2: 44 4 -8 -5
ID 44 for example, has two negative values but I would like to include all of their rows from the main datatable. Basically all rows with a negative value in any of their columns I would like to add to a new datatable that contains all their information.
The actual dataset I'm working with has ~50 stage columns, so the most efficient solution is what I'm after.