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

melt adds a number to value.name when using patterns #1346

Closed
jaapwalhout opened this issue Sep 20, 2015 · 2 comments
Closed

melt adds a number to value.name when using patterns #1346

jaapwalhout opened this issue Sep 20, 2015 · 2 comments
Assignees
Labels
Milestone

Comments

@jaapwalhout
Copy link

When using the patterns argument with one value column in melt a number (1) is added to the value name. A reproducible example:

ranks <- structure(list(id = c("w01", "w02", "w03", "w04", "w05", "w06", 
"w07", "w08", "w09", "w10", "w11", "w12", "w13", "w14", "w15", 
"w16", "w17", "w18", "w19", "w20", "w21", "w22", "w23", "w24", 
"w25"), g1 = 1:25, g2 = c(21L, 22L, 23L, 24L, 25L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L), g3 = c(16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 
24L, 25L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 
13L, 14L, 15L), g4 = c(11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 
19L, 20L, 21L, 22L, 23L, 24L, 25L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L), g5 = c(6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 1L, 2L, 
3L, 4L, 5L)), .Names = c("id", "g1", "g2", "g3", "g4", "g5"), row.names = c(NA, 
-25L), class = c("data.table", "data.frame"))

The following two melt operation should give the same output (but don't):

rnk1 <- melt(ranks, id="id",
             measure.vars = patterns("^g"), 
             variable.name = "gid", 
             value.name = "rank")
rnk2 <- melt(ranks, id="id", 
             measure.vars = c("g1","g2","g3","g4","g5"), 
             variable.name = "gid", 
             value.name = "rank")

The output of names:

> names(rnk1)
[1] "id"    "gid"   "rank1"
> names(rnk2)
[1] "id"   "gid"  "rank"

I expected that rnk1 would have the same names as rnk2

The cause of this behavior is from this part of the melt function:

if (is.list(measure.vars)) {
    if (length(value.name) == 1L) 
        value.name = paste(value.name, seq_along(measure.vars), 
            sep = "")
}

As @arunsrinivasan mentioned in an SO chat, it could be solved by replacing is.list(measure.vars) with is.list(measure.vars) && length(measure.vars) > 1L).

@jaapwalhout jaapwalhout changed the title melt add number to value.name when using patterns melt adds a number to value.name when using patterns Sep 21, 2015
@arunsrinivasan arunsrinivasan added this to the v1.9.8 milestone Sep 22, 2015
@arunsrinivasan arunsrinivasan self-assigned this Sep 22, 2015
@arunsrinivasan
Copy link
Member

Thanks @jaapwalhout, should be fixed now. Please verify.

@jaapwalhout
Copy link
Author

Tested: it works.

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

No branches or pull requests

2 participants