Skip to content

dcast outputs column name V1 for empty string #5605

@tdhock

Description

@tdhock

Hi! I expected that dcast(DT, .~column) should always output a data table that has columns named unique(paste(DT$column)) but here is a counter-example:

> x=c("NA","",NA,"foo");DT=data.table(x);out=dcast(DT,.~x,length)
> x
[1] "NA"  ""    NA    "foo"
> names(out)
[1] "."   "NA"  "V1"  "NA"  "foo"
> out
   . NA V1 NA foo
1: .  1  1  1   1

The issue above is that the empty string value is mapped to the column name V1, which is confusing. I would propose to fix by changing that output column name to empty string, as below:

> setnames(out,c(".","NA","","NA","foo"))
> out
   . NA   NA foo
1: .  1 1  1   1

Another thing I noticed is that both NA (missing value) and "NA" (string) map to two different columns (with the same name, "NA"), is that normal? (should there be a warning if dcast outputs columns with the same name?)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions