Say I have this df
require(data.table)
df <- data.table(a = 1:2
, b = 3:4
, c = 5:6
); df
a b c
1: 1 3 5
2: 2 4 6
and I wanted to fetch 2 columns at a time e.g. c('a', 'b') then c('a', 'c'). I tried:
x <- c('b', 'c')
for (i in x)
{
print( df[, c('a', i)] )
}
instead of returning a data table with the specified columns it only returned the vector of the specified column names:
[1] "a" "b"
[1] "a" "c"
What am I doing wrong? Thank you.