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 could collapse to multiple variable/value columns #828

Closed
arunsrinivasan opened this issue Sep 24, 2014 · 4 comments
Closed

melt could collapse to multiple variable/value columns #828

arunsrinivasan opened this issue Sep 24, 2014 · 4 comments
Assignees
Milestone

Comments

@arunsrinivasan
Copy link
Member

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.

@arunsrinivasan
Copy link
Member Author

Implemented here. Will add README and then close. More specifically commit 9833fe0.

@brodieG
Copy link

brodieG commented Nov 7, 2014

Arun, I think you need to add one more column to the example, otherwise it will look like something like:

library(reshape2)
melt(DT, id.vars=c("id", "c"))

will do the job. The tricky case is:

DT <- data.table(id=1:5, a=6:10, b=11:15, c=letters[1:5], d=letters[6:10])
melt(DT, id.var="id", measure.var=list(c("a", "b"), c("c", "d")))

Took me a minute to figure out why this was needed.

@arunsrinivasan
Copy link
Member Author

@brodieG thanks. The examples in ?melt cover this quite well, I believe (feedbacks welcome). That was just for me to get a better picture. On your example, right. That was one of the main reasons I wanted to implement this.

@arunsrinivasan
Copy link
Member Author

  • Update documentation
  • Need for variable.name = NULL? (In discussion with Ananda).

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

No branches or pull requests

2 participants