Subsetting columns works on data.frame but not on data.table

一笑奈何 提交于 2019-12-11 13:16:45

问题


I can select a few columns from a data.frame:

> z[c("events","users")]
     events  users
1  26246016 201816
2    942767 158793
3  29211295 137205
4  30797086 124314

but not from a data.table:

> best[c("events","users")]
Starting binary search ...Error in `[.data.table`(best, c("events", "users")) : 
  typeof x.pixel_id (integer) != typeof i.V1 (character)
Calls: [ -> [.data.table

What do I do? Is there a better way than to turn the data.table back into a data.frame?


回答1:


Column subsetting should be done in j, not in i. Do instead:

DT[, c("x", "y"), with=FALSE]

The with=FALSE makes sure that j is not evaluated as an expression. This is basically the idiomatic way for column subset if you're looking for similar means as one would subset on a data.frame.

Check this presentation (slide 4) to get an idea of how to read a data.table syntax (more like SQL). That'll help convince you that it makes more sense for providing columns in j - equivalent of SELECT in SQL.




回答2:


Given that you're looking for a data.table back you should use list rather than c in the j part of the call.

z[, list(events,users)]    # first comma is important

Note that you don't need the quotes around the column names.



来源:https://stackoverflow.com/questions/21170030/subsetting-columns-works-on-data-frame-but-not-on-data-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!