YTW
YTW

Reputation: 27

Difference between indexing columns in data.table and data.frame

DF = data.frame(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)
DT = data.table(x=rep(c("a","b","c"),each=3), y=c(1,3,6), v=1:9)

When I write DF[2,3],it shows 2 because that is row 2, col 3. But as for DT[2,3], it shows 3? I would like to know how this value 3 is arrived at, and if I want row 2 col 3. What should I do?

Upvotes: 0

Views: 182

Answers (1)

dww
dww

Reputation: 31452

This is a common source of confusion in data.table. the second j argument in [.data.table is "evaluated within the frame of the data.table; i.e., it sees column names as if they are variables", so the 3 that you got unexpectedly is the result of evaluating the value 3 that you passed.

you can access the 3rd column by name

DT[2,v]

or by index if you specify with=FALSE

DT[2,3, with=FALSE]

Type ?data.table at the command line to see the help page for this

Upvotes: 3

Related Questions