Hi all,
I want to filter out rows from a data table (DT) using values from a vector (goodHosp). I'm wondering what the best way to go about doing it.
DT <- data.table(patients = 1:5, treatment = letters[1:5],
hospital = c(".yyy", ".yyy.bbb", ".zzz", ".yyy.www", ".uuu")
, response = rnorm(5))
goodHosp <- c("yyy", "uuu")
My coworker wrote a for loop. It seems to work if the vector only has 1 value but not for multiple values as the below example has 2 values.
for (i in 1:length(goodHosp)){
if(i == 1)
a_DT <- DT[DT$hospital %like% goodHosp[1] ]
else
a_DT <- funion(DT, DT[hospital %like% goodHosp[i] ])
}
It returns all values as where I would expect it to filters out row #3 where it doesn't matched the value in goodHosp vector. This means that the "else" part is not working correctly.
patients treatment hospital response
1: 1 a .yyy 0.6886801
2: 2 b .yyy.bbb 2.0524934
3: 3 c .zzz 0.8979818
4: 4 d .yyy.www 0.3883533
5: 5 e .uuu 0.5332226
I would like to understand what doesn't work in the for loop. I also want to know if there is a more elegant and effective way to do this by using apply() or purrr that I'm not too familiar with yet.
Thanks in advance!