子集设置列适用于data.frame,但不适用于data.table [英] Subsetting columns works on data.frame but not on data.table
问题描述
我可以从 data.frame
中选择几个列:
> z [c(events,users)]
事件用户
1 26246016 201816
2 942767 158793
3 29211295 137205
4 30797086 124314
,但不是 data.table
:
>最好的[c(events,users)]
开始二进制搜索...错误在`[.data.table`(best,c(events,users)):
typeof x.pixel_id(integer)!= typeof i.V1(character)
Calls:[ - > [.data.table
我该怎么办?
有更好的方法比 turn data.table
返回 data.frame
?
DT [,c(x,y),with = FALSE]
<= code>
j
不作为表达式求值。
检查此演示文稿(幻灯片4)以了解如何阅读
data.table
语法(更像SQL)。这将有助于说服您在j
中提供列 - 在SQL中等效于SELECT,这样更有意义。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 adata.frame
?解决方案Column subsetting should be done in
j
, not ini
. Do instead:DT[, c("x", "y"), with=FALSE]
The
with=FALSE
makes sure thatj
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 inj
- equivalent of SELECT in SQL.这篇关于子集设置列适用于data.frame,但不适用于data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!