0

I'm trying to select one column of an R datatable, without much luck:

test <- data.table(col1 = c("A", "B", "C", "D"), col2 = c(1,2,3,4))
idx  <- 1
test[,idx]

The result is [1] 1 instead of what I assume would be the output of test[,1].

How come?

Thanks for all help!

Gabriel
  • 121
  • 4
  • 1
    https://stackoverflow.com/questions/20043313/extract-a-column-from-a-data-table-as-a-vector-by-position/20043412#20043412 – Roland Mar 27 '18 at 12:27

1 Answers1

1

We can use the double dots (..) to extract the columns saved in an object

test[, ..idx]
#   col1
#1:    A
#2:    B
#3:    C
#4:    D
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Doesn't work for me unfortunately: "Error in eval(jsub, SDenv, parent.frame()) : object '..idx' not found. Any help with that?" Thanks! – Gabriel Mar 27 '18 at 12:58
  • 1
    @Gabriel What is your data.table version. It should be updated to new version – akrun Mar 27 '18 at 12:59