Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dcast with empty data.table #1215

Closed
dbetebenner opened this issue Jul 8, 2015 · 4 comments · Fixed by #5118
Closed

dcast with empty data.table #1215

dbetebenner opened this issue Jul 8, 2015 · 4 comments · Fixed by #5118

Comments

@dbetebenner
Copy link

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
@arunsrinivasan
Copy link
Member

I agree with base::reshape's behaviour. However, data.table::dcast has to be consistent with reshape2::dcast, not base::reshape. If rehape2::dcast, changes its behaviour, then yes. But I doubt it's possible anymore.

@dbetebenner
Copy link
Author

Thanks for responding Arun,

That makes sense. I've created a wrapper function to accomplish what I needed.

Best

@arunsrinivasan
Copy link
Member

👍

@jangorecki
Copy link
Member

@arunsrinivasan do we need to be consistent to reshape2 here? we already shifting away from reshape2 recently, see #3549
This SO https://stackoverflow.com/questions/57722355/r-shiny-is-breaking-down-by-using-dcast-from-data-table
is another example that it would be useful.
Feel free to close if you don't agree.

@jangorecki jangorecki reopened this Sep 2, 2019
@jangorecki jangorecki added reshape dcast melt and removed question labels Mar 2, 2020
@jangorecki jangorecki added this to the 1.12.9 milestone Apr 3, 2020
@mattdowle mattdowle modified the milestones: 1.13.1, 1.13.3 Oct 17, 2020
@jangorecki jangorecki modified the milestones: 1.14.9, 1.15.0 Oct 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants