镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

String coercion inserts spaces, as.matrix.data.table #4209

Open

Description

@alistaire47

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)))
     a   b      
[1,] "A" "FALSE"
[2,] "B" "TRUE" 
> as.matrix(data.table(a = LETTERS[1:2], b = c(FALSE, TRUE)))
     a   b      
[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.

Activity

  1. MichaelChirico commented on Jan 30, 2020

    @MichaelChirico
    Member

    as.matrix.data.frame uses as.character for logical columns, whereas as.matrix.data.table uses format -- format adds the space.

    @sritchie73 is this covered by your ongoing #4144? I was unable to install & test myself for the moment

  2. MichaelChirico commented on Jan 30, 2020

    @MichaelChirico
    Member

    In fact, it seems this is something that was fixed quite recently in base R itself!

    wch/r-source@bd3b7ef

    So I guess our issue is a vestige of the same logic being copied over from as.matrix.data.frame many years ago

  3. sritchie73 commented on Jan 30, 2020

    @sritchie73
    Contributor

    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"
    
  4. changed the title [-]String coercion inserts spaces[/-] [+]String coercion inserts spaces, as.matrix.data.table[/+] on Apr 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions