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

dcast outputs column name V1 for empty string #5605

Open
tdhock opened this issue Feb 28, 2023 · 3 comments · May be fixed by #6795
Open

dcast outputs column name V1 for empty string #5605

tdhock opened this issue Feb 28, 2023 · 3 comments · May be fixed by #6795
Labels

Comments

@tdhock
Copy link
Member

tdhock commented Feb 28, 2023

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?)

@tdhock tdhock added the reshape dcast melt label Feb 28, 2023
@ben-schwen
Copy link
Member

I guess the initial intention was to avoid duplicate names similar to make.names(names, unique=TRUE), hence, I find it quite surprising that you can sneak in the double "NA".

@Divendra2006
Copy link

Hi @tdhock,

I came across this issue, which was opened in March 2023, and I’m interested in contributing. However, I wanted to check if this issue is still relevant and needs to be addressed.

If it’s still valid, I’d be happy to work on it. Please let me know.

@tdhock
Copy link
Member Author

tdhock commented Jan 24, 2025

yes still relevant, please go ahead.

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

Successfully merging a pull request may close this issue.

3 participants