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"))
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")
if (is.list(measure.vars)) {
if (length(value.name) == 1L)
value.name = paste(value.name, seq_along(measure.vars),
sep = "")
}
When using the
patternsargument with one value column inmelta number (1) is added to the value name. A reproducible example:The following two
meltoperation should give the same output (but don't):The output of
names:I expected that
rnk1would have the same names asrnk2The cause of this behavior is from this part of the
meltfunction:As @arunsrinivasan mentioned in an SO chat, it could be solved by replacing
is.list(measure.vars)withis.list(measure.vars) && length(measure.vars) > 1L).