-
Notifications
You must be signed in to change notification settings - Fork 991
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
melt.data.table should offer variable
to match on the name, rather than the number
#3396
Comments
I'm starting at this and not seeing what's incorrect? And maybe duplicate of #2551? The functionality is certainly useful... |
I think it's the same as that one though it didn't appear in the searches and I think my reprex is clearer. I think the result of the second melt should be: #> 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 NA
#> 12: 2 3 402 NA
#> 13: 3 3 403 NA
#> 14: 4 3 404 NA
#> 15: 5 3 405 NA
#> 16: 1 4 NA 501
#> 17: 2 4 NA 502
#> 18: 3 4 NA 503
#> 19: 4 4 NA 504
#> 20: 5 4 NA 505 Created on 2019-02-13 by the reprex package (v0.2.1) |
|
Arun, happy to leave the existing functionality as-is (was planning to), but I do think that the second example is a bug: there should be four levels, not three. |
@HughParsonage What you require could be achieved by extending the functionality of For e.g., melt(dt, id="a", measure=patterns("b$", "c$", fill=TRUE)) or |
Regarding syntax for applying strict matching, I guess it would be necessary to identify which part of the string to match across the patterns. This could be done in a capture group that additionally could be used to populate the
(The latter example from #3487 highlighted here to show the connection I see between the two issues to the OP @jsams ) So the two |
hi this is related to #4027 and fixed by #4720, not merged into master yet but can be used via remotes::install_github(c("Rdatatable/data.table@fix4027", "tdhock/nc@multiple-fill"))
#> Skipping install of 'data.table' from a github remote, the SHA1 (4c5810c2) has not changed since last install.
#> Use `force = TRUE` to force installation
#> Skipping install of 'nc' from a github remote, the SHA1 (11b61f8e) has not changed since last install.
#> Use `force = TRUE` to force installation
library(data.table)
DT <-
data.table(x = 1:5,
y_No = 101:105,
`y_$`= 1011:1015,
z_No = 201:205,
`z_$` = 301:305)
nc::capture_melt_multiple(
DT,
variable_new="[yz]",
"_",
column=".*", function(x)ifelse(x=="$", "Dollar", "Number"))
#> x variable_new Dollar Number
#> 1: 1 y 1011 101
#> 2: 2 y 1012 102
#> 3: 3 y 1013 103
#> 4: 4 y 1014 104
#> 5: 5 y 1015 105
#> 6: 1 z 301 201
#> 7: 2 z 302 202
#> 8: 3 z 303 203
#> 9: 4 z 304 204
#> 10: 5 z 305 205
DT2 <-
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)
nc::capture_melt_multiple(
DT2,
variable_new=".*",
"_",
column=".*", function(x)ifelse(x=="$", "Dollar", "Number"),
fill=TRUE)
#> x variable_new Dollar Number
#> 1: 1 w1 401 NA
#> 2: 2 w1 402 NA
#> 3: 3 w1 403 NA
#> 4: 4 w1 404 NA
#> 5: 5 w1 405 NA
#> 6: 1 w2 NA 501
#> 7: 2 w2 NA 502
#> 8: 3 w2 NA 503
#> 9: 4 w2 NA 504
#> 10: 5 w2 NA 505
#> 11: 1 y 1011 101
#> 12: 2 y 1012 102
#> 13: 3 y 1013 103
#> 14: 4 y 1014 104
#> 15: 5 y 1015 105
#> 16: 1 z 301 201
#> 17: 2 z 302 202
#> 18: 3 z 303 203
#> 19: 4 z 304 204
#> 20: 5 z 305 205 |
Let's wait till it is merged |
hi again the solution using only the new data.table::melt (without nc/regex) looks like remotes::install_github("Rdatatable/data.table@fix4027")
#> Skipping install of 'data.table' from a github remote, the SHA1 (4c5810c2) has not changed since last install.
#> Use `force = TRUE` to force installation
library(data.table)
DT2 <-
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)
DT2.tall <- melt(DT2, measure=list(
Dollar=c("w1_$", NA, "y_$", "z_$"),
Number=c(NA, "w2_No", "y_No", "z_No")))
DT2.tall[, variable_new := c("w1", "w2", "y", "z")[variable] ]
DT2.tall
#> x variable Dollar Number variable_new
#> 1: 1 1 401 NA w1
#> 2: 2 1 402 NA w1
#> 3: 3 1 403 NA w1
#> 4: 4 1 404 NA w1
#> 5: 5 1 405 NA w1
#> 6: 1 2 NA 501 w2
#> 7: 2 2 NA 502 w2
#> 8: 3 2 NA 503 w2
#> 9: 4 2 NA 504 w2
#> 10: 5 2 NA 505 w2
#> 11: 1 3 1011 101 y
#> 12: 2 3 1012 102 y
#> 13: 3 3 1013 103 y
#> 14: 4 3 1014 104 y
#> 15: 5 3 1015 105 y
#> 16: 1 4 301 201 z
#> 17: 2 4 302 202 z
#> 18: 3 4 303 203 z
#> 19: 4 4 304 204 z
#> 20: 5 4 305 205 z |
pure data.table solution using #4731 remotes::install_github("Rdatatable/data.table@melt-custom-variable")
#> Skipping install of 'data.table' from a github remote, the SHA1 (c02fa9e8) has not changed since last install.
#> Use `force = TRUE` to force installation
library(data.table)
DT2 <-
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(DT2, measure.vars=measure(
variable_new, value.name=function(x)c("$"="Dollar","No"="Number")[x]))
#> x variable_new Number Dollar
#> 1: 1 y 101 1011
#> 2: 2 y 102 1012
#> 3: 3 y 103 1013
#> 4: 4 y 104 1014
#> 5: 5 y 105 1015
#> 6: 1 z 201 301
#> 7: 2 z 202 302
#> 8: 3 z 203 303
#> 9: 4 z 204 304
#> 10: 5 z 205 305
#> 11: 1 w1 NA 401
#> 12: 2 w1 NA 402
#> 13: 3 w1 NA 403
#> 14: 4 w1 NA 404
#> 15: 5 w1 NA 405
#> 16: 1 w2 501 NA
#> 17: 2 w2 502 NA
#> 18: 3 w2 503 NA
#> 19: 4 w2 504 NA
#> 20: 5 w2 505 NA |
Currently, using
patterns
makes the variable column an integer, which is both a bit awkward ...... and potentially error-prone, e.g.
Created 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.
The text was updated successfully, but these errors were encountered: