Skip to content

dcast with empty data.table #1215

@dbetebenner

Description

@dbetebenner

I see dcast throws an error when one submits an empty data.table. This appear to be consistent with an error that reshape2:::dcast throws with an empty data.frame, but inconsistent with how reshape deals with empty data.frames (returns an empty data.frame).

Any possibility for changing the dcast behavior to return an empty data.table?

Here's an example:

> require(data.table)
Loading required package: data.table
data.table 1.9.5  For help type ?data.table or https://github.com/Rdatatable/data.table/wiki
> test.dt <- data.table(ID=rep(1:2, each=2), X=rnorm(4), TIME=rep(1:2, 2))
> test.dt


   ID         X TIME
1:  1 0.5567229    1
2:  1 0.8330417    2
3:  2 0.2159679    1
4:  2 0.3763645    2

Non-empty

> reshape(test.dt, idvar="ID", timevar="TIME", direction="wide")
   ID       X.1       X.2
1:  1 0.5567229 0.8330417
2:  2 0.2159679 0.3763645
> dcast(test.dt, ID ~ TIME, value.var="X")
   ID         1         2
1:  1 0.5567229 0.8330417
2:  2 0.2159679 0.3763645

Empty

> reshape(subset(test.dt, ID==3), idvar="ID", timevar="TIME", direction="wide")
Empty data.table (0 rows) of 1 col: ID```
> dcast(subset(test.dt, ID==3), ID ~ TIME, value.var="X")
Error in dcast.data.table(subset(test.dt, ID == 3), ID ~ TIME, value.var = "X") : 
  Can not cast an empty data.table

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions