-
Notifications
You must be signed in to change notification settings - Fork 1k
Closed
Labels
enhancementfeature requestreshapedcast meltdcast melttop requestOne of our most-requested issuesOne of our most-requested issues
Milestone
Description
Currently, using patterns makes the variable column an integer, which is both a bit awkward ...
library(data.table)
DT <-
data.table(x = 1:5,
y_No = 101:105,
`y_$`= 1011:1015,
z_No = 201:205,
`z_$` = 301:305)
DT_melt <-
melt.data.table(DT,
id.vars = "x",
measure.vars = patterns("Dollar" = "\\$$",
"Number" = "No$"))
DT_melt
#> x variable Dollar Number
#> 1: 1 1 1011 101
#> 2: 2 1 1012 102
#> 3: 3 1 1013 103
#> 4: 4 1 1014 104
#> 5: 5 1 1015 105
#> 6: 1 2 301 201
#> 7: 2 2 302 202
#> 8: 3 2 303 203
#> 9: 4 2 304 204
#> 10: 5 2 305 205
# Intended
DT_melt[, variable_new := c("y", "z")[variable]][]
#> x variable Dollar Number variable_new
#> 1: 1 1 1011 101 y
#> 2: 2 1 1012 102 y
#> 3: 3 1 1013 103 y
#> 4: 4 1 1014 104 y
#> 5: 5 1 1015 105 y
#> 6: 1 2 301 201 z
#> 7: 2 2 302 202 z
#> 8: 3 2 303 203 z
#> 9: 4 2 304 204 z
#> 10: 5 2 305 205 z... and potentially error-prone, e.g.
library(data.table)
DT <-
data.table(x = 1:5,
y_No = 101:105,
`y_$`= 1011:1015,
z_No = 201:205,
`z_$` = 301:305,
`w1_$` = 401:405,
w2_No = 501:505)
melt.data.table(DT,
id.vars = "x",
measure.vars = patterns("Dollar" = "\\$$",
"Number" = "No$"))
#> x variable Dollar Number
#> 1: 1 1 1011 101
#> 2: 2 1 1012 102
#> 3: 3 1 1013 103
#> 4: 4 1 1014 104
#> 5: 5 1 1015 105
#> 6: 1 2 301 201
#> 7: 2 2 302 202
#> 8: 3 2 303 203
#> 9: 4 2 304 204
#> 10: 5 2 305 205
#> 11: 1 3 401 501 # incorrect
#> 12: 2 3 402 502 # incorrect
#> 13: 3 3 403 503 # incorrect
#> 14: 4 3 404 504 # incorrect
#> 15: 5 3 405 505 # incorrectCreated on 2019-02-13 by the reprex package (v0.2.1)
Can offer a PR if this is a worthwhile feature. Otherwise can just attempt the bugfix.
jaapwalhout, kendonB, Henrik-P, chengchch, Roadrunner-one and 5 more
Metadata
Metadata
Assignees
Labels
enhancementfeature requestreshapedcast meltdcast melttop requestOne of our most-requested issuesOne of our most-requested issues