Skip to content

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

Closed

Description

@jaapwalhout

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

Activity

  1. changed the title [-]melt add number to value.name when using patterns[/-] [+]melt adds a number to value.name when using patterns[/+] on Sep 21, 2015
  2. added this to the v1.9.8 milestone on Sep 22, 2015
  3. arunsrinivasan commented on Sep 23, 2015

    @arunsrinivasan
    Member

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

  4. jaapwalhout commented on Sep 24, 2015

    @jaapwalhout
    Author

    Tested: it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions