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

WISH: rbind on data.frames with 0 columns would preserve rows #77

Open
karoliskoncevicius opened this issue Sep 10, 2018 · 3 comments
Open

Comments

@karoliskoncevicius
Copy link

With matrix objects rbind works as expected:

mat1 <- matrix(nrow=2, ncol=0)
mat2 <- matrix(nrow=2, ncol=0)

> dim(rbind(mat1, mat2))
[1] 4 0

Once turned into data.frames the rows are dropped:

> dim(rbind(as.data.frame(mat1), as.data.frame(mat2)))
[1] 0 0

However cbind works as expected:

> dim(cbind(as.data.frame(mat1), as.data.frame(mat2)))
[1] 2 0
@jangorecki
Copy link

you might find following discussion related: Rdatatable/data.table#2422

@karoliskoncevicius
Copy link
Author

karoliskoncevicius commented Aug 6, 2019

@jangorecki thank you for the comment and the link.

You seem to argue that zero-row/column matrices should be a thing and nonzero-row zero-column data.frames should not be a thing. I would tend to agree, however below is an example of scenario I encountered where such data.frames are a good default edge case.

For my personal uses I am developing an "annotated matrix" object. That is - a matrix with persistent annotations of rows and columns. This object holds annotation about rows and columns in two separate data.frames. Once you select the row of such a matrix - row annotations are automatically subsetted and vice-versa.

Under such conditions a data.frame with non zero rows and zero columns is quite useful - it's a state of there not being any meta-data present. So for a matrix with n rows and m columns - the column-annotation data.frame would have n rows and 0 columns.

I found that having this behaviour works well with various cbind, rbind, etc operations by default without resorting to hacks. And I would have to add some ad-hoc solutions in order to make dim(0, 0) work.

This is precisely the issue I encountered and reported here. When I cbind my special matrices - I can simply also do cbind on their annotations. However this didn't work when I did an rbind, which was confusing (but documented) behaviour in base R.

@jangorecki
Copy link

I would rather prefer to change cbind.data.frame to match rbind.data.frame behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants