I have a dataset that looks something like this.
df <- tibble::tribble(
~name, ~x, ~y, ~z,
"N/A", 1, "testSmith", -100,
"N A", 3, "NOt available", -99,
"test Smith", NA, "test Smith", -98,
"Not Available", -99, "25", -101,
"test Smith", -98, "28", -1)
I would like to create a new data.table that keeps all the rows the string "test".
The final dataset should look something like this
name x y z
<chr> <dbl> <chr> <dbl>
1 N/A 1 testSmith -100
2 test Smith NA test Smith -98
3 test Smith -98 28 -1
I could do this column by column like this
setDT(df)[name%like%"test"|y%like%"test"]
The problem with this approach is that I have hundreds of string variables and I would like to find a more compact approach. I tried the followings but they do not work
chvar <- keep(trai,is.character)%>%names()
setDT(df)[chvar%like%"test"]#error
setDT(df)[(chvar)%like%"test"]#error
setDT(df)[.(chvar)%like%"test"]#empty dt
Does someone know how I could do it in a quick and efficient way?
Thanks a lot for your help