I am learning data.table and trying to filter certain columns by using a vector containing a set of column names.
> dt <- data.table(A=1:5, B=2:6, C=3:7)
> dt
A B C
1: 1 2 3
2: 2 3 4
3: 3 4 5
4: 4 5 6
5: 5 6 7
>
> list <- c("A", "B")
> dt[ ,list, with=FALSE]
A B
1: 1 2
2: 2 3
3: 3 4
4: 4 5
5: 5 6
>
This works fine and filter columns. However, the "missing" item in the list will return an error:
> list <- c("A", "B", "D")
> dt[ ,list, with=FALSE]
Error in `[.data.table`(dt, , list, with = FALSE) :
column(s) not found: D
How can I ignore the missing column name from the list and return just existing columns from the dt data.table?