I have my data.table DT which I'd like to filter and keep only the rows where any of the values in that row contain the string "tokeep".
library(data.table)
cola <- c(1:5)
colb <- c(letters[1:4], "tokeep")
dt <- data.table(cola, colb)
dt
cola colb
1: 1 a
2: 2 b
3: 3 c
4: 4 d
5: 5 tokeep
expected result :
dt[grepl("tokeep", colb)]
cola colb
1: 5 tokeep
However I don't know in which column tokeep will be found. I have tried using .SD
in i
like this
dt[any(grepl("tokeep", .SD))]
Empty data.table (0 rows) of 2 cols: cola,colb
Also, can't figure out the following.
> dt[,print(any(grepl("tokeep", .SD)))]
[1] TRUE
[1] TRUE
Shouldn't it be FALSE, TRUE
since "tokeep"
only exists in colb
?