子集设置列适用于data.frame,但不适用于data.table [英] Subsetting columns works on data.frame but not on data.table

查看:273
本文介绍了子集设置列适用于data.frame,但不适用于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

c> c> c> c> $ c>。请改为:

  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 a data.frame?

解决方案

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.

这篇关于子集设置列适用于data.frame,但不适用于data.table的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆