3

How to slice column with character object in data table in R? I have tried this piece of codes but it does not work

i <- "Spot"    
DT[1,i]

I want the code to return the result as the same as

DT[1,"Spot"]

Can I ask what is the correct way to do it with the character obejct i instead of the character itself "Spot"?

Waldi
  • 39,242
  • 6
  • 30
  • 78
Ziroque
  • 47
  • 7

1 Answers1

3

Try with

DT[1, ..i]

Or use

DT[1, i, with = FALSE]

data

library(data.table)
DT <- data.table(Spot = 1:5, col2 = 6:10)
akrun
  • 874,273
  • 37
  • 540
  • 662