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.data.table inconsistent with reshape2::melt #1699

Closed
MichaelChirico opened this issue May 11, 2016 · 6 comments
Closed

melt.data.table inconsistent with reshape2::melt #1699

MichaelChirico opened this issue May 11, 2016 · 6 comments
Milestone

Comments

@MichaelChirico
Copy link
Member

MichaelChirico commented May 11, 2016

See this SO Q&A.

Here's what works in reshape2:

buyer      <- c("A", "A", "B", "A", "B", "C")
seller     <- c("C", "B", "C", "B", "C", "A")
negotiator <- c("B", "C", "D", "D", "A", "B")

df <- data.frame(buyer, seller, negotiator)
reshape2::melt(df,0)
#      variable value
#1       buyer     A
#2       buyer     A
#3       buyer     B
#4       buyer     A
#5       buyer     B
#6       buyer     C
#7      seller     C
#8      seller     B
#9      seller     C
#10     seller     B
#11     seller     C
#12     seller     A
#13 negotiator     B
#14 negotiator     C
#15 negotiator     D
#16 negotiator     D
#17 negotiator     A
#18 negotiator     B

But that's an error in melt.data.table:

setDT(df)
melt(df, 0)

Error in melt.data.table(df, 0) :
One or more values in id.vars is invalid.

This is fine, as we can simply set measure.vars = names(df), but IIUC we're aiming for melt.data.table to subsume reshape2::melt.

@arunsrinivasan
Copy link
Member

Michael, thanks. But I'm quite happy with current behaviour of returning an error. melt(df, -1) returns something else. It's hard to keep up with undocumented behaviours.

@MichaelChirico
Copy link
Member Author

Sounds good to me. Just figured I'd report. I also noticed reshape2::melt(df, -1) doing strange things.

@MichaelChirico
Copy link
Member Author

MichaelChirico commented May 12, 2016

Perhaps worth adding a note to ?melt.data.table??

@arunsrinivasan
Copy link
Member

Sure. A separate Note section should be fine.

@MichaelChirico
Copy link
Member Author

Was putting this off because I first wanted to dive into reshape2 to figure out what's going on. For posterity, here's the relevant snippet from reshape2:::melt_check:

varnames <- names(data)
if (!missing(id.vars) && is.numeric(id.vars)) {
    id.vars <- varnames[id.vars]
}
# ...
measure.vars <- setdiff(varnames, id.vars)

So id.vars becomes character(0) when id.vars = 0, and in turn measure.vars becomes exactly varnames. This also helps explain what's happening when id.vars = -1 -- id.vars becomes varnames[-1], i.e., all but the last column.

@mattdowle mattdowle added this to the v1.10.6 milestone Aug 10, 2017
@mattdowle
Copy link
Member

Closed by 6437417 in #2290.
Think the space between # and issue number in the commit message prevented auto close and link.

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

3 participants