You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When coercing from a data.table to a matrix and types are coerced (yes this is bad and shouldn't be done, so this is definitely low-priority), a data.table inserts spaces whereas a data.frame does not:
> as.matrix(data.frame(a=LETTERS[1:2], b= c(FALSE, TRUE)))
ab
[1,] "A""FALSE"
[2,] "B""TRUE"> as.matrix(data.table(a=LETTERS[1:2], b= c(FALSE, TRUE)))
ab
[1,] "A""FALSE"
[2,] "B"" TRUE"
I assume this is a result of C++ typing somehow, but I don't know enough to make a PR, sorry.
This is fixed in #4144 , this is the output I see on my working test branch:
a b
[1,] "A" "FALSE"
[2,] "B" "TRUE"
The current version of data.table on CRAN simply uses base::unlist(), while #4144 is implemented in C and uses our internal memrecycle function which has the correct desired behaviour:
> dt = data.table(a = LETTERS[1:2], b = c(FALSE, TRUE))
> rbind(dt[,.(a)], dt[,.(b)], use.names=FALSE)[,a] # rbindlist uses memrecycle for type coercion
[1] "A" "B" "FALSE" "TRUE"
Activity
MichaelChirico commentedon Jan 30, 2020
as.matrix.data.frameusesas.characterfor logical columns, whereasas.matrix.data.tableusesformat--formatadds the space.@sritchie73 is this covered by your ongoing #4144? I was unable to install & test myself for the moment
MichaelChirico commentedon Jan 30, 2020
In fact, it seems this is something that was fixed quite recently in
baseR itself!wch/r-source@bd3b7ef
So I guess our issue is a vestige of the same logic being copied over from
as.matrix.data.framemany years agosritchie73 commentedon Jan 30, 2020
This is fixed in #4144 , this is the output I see on my working test branch:
The current version of data.table on CRAN simply uses
base::unlist(), while #4144 is implemented in C and uses our internal memrecycle function which has the correct desired behaviour:[-]String coercion inserts spaces[/-][+]String coercion inserts spaces, as.matrix.data.table[/+]