Skip to content

Ability to explicitly pass index names in melt.data.table measure.vars argument #4027

@vspinu

Description

@vspinu

Currently melt with multiple melting patterns works as expected only when data has a regular grid pattern. If some of the variables are missing the order of the output is scrambled.

For example:

(dt <- data.table(id = 1, a.1 = 1, a.3 = 3, b.1 = 1, b.2 = 2, b.3 = 3))
##    id a.1 a.3 b.1 b.2 b.3
## 1:  1   1   3   1   2   3

melt.data.table(dt, "id" , measure.vars = list(a = 2:3, b = 4:6), variable.name = "ix")
##    id ix  a b
## 1:  1  1  1 1
## 2:  1  2  3 2
## 3:  1  3 NA 3

But what I really want is the ix of a.3 to be 3, not 2.

##    id ix  a b
## 1:  1  1  1 1
## 2:  1  2 NA 2
## 3:  1  3  3 3

I was hoping that explicit index names in the measure.vars would be taken into account but they are currently not:

melt.data.table(dt, "id" ,
                measure.vars = list(a = c("1" = 2, "3" = 3),
                                    b = c("4" = 4, "5" = 5, "6" = 6)),
                variable.name = "ix")
##    id ix  a b
## 1:  1  1  1 1
## 2:  1  2  3 2
## 3:  1  3 NA 3

I am fairly confident that this is related (or maybe even the same as #1739) but the example and discussion there is a bit cryptic.

Would you consider adding the ability to explicitly pass index names?

Thanks!

Details
R Under development (unstable) (2019-08-06 r76924)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 18.04.3 LTS

Matrix products: default
BLAS:   /home/vspinu/bin/R-latest/lib/libRblas.so
LAPACK: /home/vspinu/bin/R-latest/lib/libRlapack.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8     LC_MONETARY=de_DE.UTF-8    LC_MESSAGES=C              LC_PAPER=de_DE.UTF-8       LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=de_DE.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  utils     datasets  grDevices methods   base     

other attached packages:
[1] data.table_1.12.4

loaded via a namespace (and not attached):
[1] compiler_3.7.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions