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

as.data.table behavior inconsistent with as.data.frame for matrix list members #833

Closed
ghost opened this issue Sep 27, 2014 · 2 comments
Closed
Assignees
Labels
Milestone

Comments

@ghost
Copy link

ghost commented Sep 27, 2014

See http://stackoverflow.com/questions/26070187/why-does-data-table-recycle-matrices-into-a-single-vector-when-data-frame-does-n for background.

Reproduce:

a.matrix <- matrix(seq_len(25),ncol = 5, nrow = 5)

a.list <- list(seq_len(5),a.matrix)

a.dt <- as.data.table(a.list)
a.df <- as.data.frame(a.list)
a.dt.df <- as.data.table(a.df)

str(a.dt)
str(a.df) # different from a.dt
str(a.dt.df) # same as a.df
@arunsrinivasan
Copy link
Member

This SO post from @rsaporta has the answer, I think.

@rsaporta
Copy link
Contributor

The issue arrises when calling as.data.table on any list(matrix)
eg:

Mat43 <- matrix(1:12, ncol=3)

list_of_matrix <- list(Mat43)

identical(as.data.frame(as.data.table(list_of_matrix)), as.data.frame(list_of_matrix))
# [1] FALSE

as.data.table(list_of_matrix)
#     V1
#  1:  1
#  2:  2
#  3:  3
#  4:  4
#  5:  5
#  6:  6
#  7:  7
#  8:  8
#  9:  9
# 10: 10
# 11: 11
# 12: 12

as.data.frame(list_of_matrix)
#   X1 X2 X3
# 1  1  5  9
# 2  2  6 10
# 3  3  7 11
# 4  4  8 12

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

No branches or pull requests

2 participants