2

Suppose I have data table:

V = data.table('x'=0, 'price1'=1:10, 'price2'=10:1)

I want to return this:

      price1 price2
 1:      1     10
 2:      2      9
 3:      3      8
 4:      4      7
 5:      5      6
 6:      6      5
 7:      7      4
 8:      8      3
 9:      9      2
10:     10      1

But

j=1:J
V[,get(paste0('price',j))]  

Does not work. I need J to be arbitrarily long, so please do not provide a workaround that directly indexes or indexes by column number, because these are both not feasible with my data because it is designed to be general. I want to call these columns by name using compact notation for an arbitrarily number of columns.

wolfsatthedoor
  • 7,163
  • 18
  • 46
  • 90
  • 2
    "I want to call these columns by name using compact notation for an arbitrarily number of columns." -- I do not know what this means. `.SD` is pretty compact... `V[,.SD, .SDcols = paste0('price', 1:2)]` (I am using "price" instead of "p" since I assume you mean to refer to columns in the example table...) – Frank May 07 '18 at 20:39
  • 1
    Good answer, I think this works, thank you – wolfsatthedoor May 07 '18 at 20:43
  • Ok cool, lots of other options mentioned in the linked q&a. – Frank May 07 '18 at 20:51

0 Answers0