Skip to content

melt could collapse to multiple variable/value columns #828

@arunsrinivasan

Description

@arunsrinivasan

As an experiment so far.

require(data.table)
DT = data.table(id=1:5, a=6:10, b=11:15, c=letters[1:5])

# expected result
ans
#     id variable1 value1 variable2 value2
#  1:  1         a      6         c      a
#  2:  2         a      7         c      b
#  3:  3         a      8         c      c
#  4:  4         a      9         c      d
#  5:  5         a     10         c      e
#  6:  1         b     11         c      a
#  7:  2         b     12         c      b
#  8:  3         b     13         c      c
#  9:  4         b     14         c      d 
#10:  5         b     15         c      e

Idea:

melt(DT, id.var="id", measure.var=list(c("a", "b"), "c"))

This is particularly useful when:

  • all elements in measure.var list are of same length and the columns we've to melt is of different type (ex: integer and character). Currently it'll all be coerced to the same type depending on hierarchy into one value column.

Similarly, cast should also be able to operate on multiple columns so that unnecessary melting can be completely avoided. But that's another FR for next release (1.9.6) #739.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions