I'm attempting to subset a data.table, small example here but will need to do bigger one.
I have the dt and the only column, code_id is the key:
> UniqueCodes
code_id
1:
2: .....
3: 10073011000001107
4: 10073911000001106
5: 10075611000001101
---
15114: x05ZX
15115: x05q2
15116: x05qb
15117: x05td
15118: x05xd
I have long list of codes I want to retain (subset) and discard the rest. So how would I subset this df and retain only x05ZX, X05qb, and X05td ? I know apriory these are the only ones I want to keep ? Thx, J.
CodePudding user response:
library(data.table)
code_id <- c(10073011000001107,
10073911000001106,
10075611000001101,
"x05ZX",
"x05q2",
"x05qb",
"x05td",
"x05xd",
"x05qb")
UniqueCodes <- data.table(code_id)
You can build a vector with the codes you want to retain
codes <- c("x05ZX", "X05qb", "X05td", "x05qb")
Then pass the vector to dt
UniqueCodes[code_id %in% codes]
## code_id
## 1: x05ZX
## 2: x05qb
## 3: x05qb