19

I have the following data.frame, called tableMS:

     X   Y        Z        T
1  375 855 455.7259 3777.856
2  395 969 347.8306   2506.7
3  449 811 309.9512 519.8513
4  451 774  278.291 717.8705
5  453 774  278.291 717.8705
6  455 774  278.291 717.8705
7  521 697  376.734 693.8541
8  529 855 455.7259 3777.856
9  531 855 455.7259 3777.856
10 609 774  278.291 717.8705

when I try to use the function melt()

MeltTable <- melt(tableMS,id=c("X","Y"))

I get the following error:

Error in match.names(clabs, names(xi)) : 
   names do not match previous names

I struggle to understand what happens, any idea?

Edit: I generated tableMS as portion of a bigger table and the output of str(tableMS) is:

'data.frame':   10 obs. of  4 variables:
$ X: num  375 395 449 451 453 455 521 529 531 609
$ Y: num  855 969 811 774 774 774 697 855 855 774
$ Z:List of 10
  ..$ : num 456
  ..$ : num 348
  ..$ : num 310
  ..$ : num 278
  ..$ : num 278
  ..$ : num 278
  ..$ : num 377
  ..$ : num 456
  ..$ : num 456
  ..$ : num 278
$ T:List of 10
  ..$ : num 3778
  ..$ : num 2507
  ..$ : num 520
  ..$ : num 718
  ..$ : num 718
  ..$ : num 718
  ..$ : num 694
  ..$ : num 3778
  ..$ : num 3778
  ..$ : num 718
Claudia
  • 996
  • 1
  • 10
  • 27

3 Answers3

58

I had this same problem, but the cause was different. I got the same error message "names do not match previous names", but it was due to using the package dplyr.

Turns out, it is a known issue with dplyr. According to the GitHub issue, it will occur on some version of dplyr and reshape but not on others.

The output from dplyr is not just a data.frame - it inherits from data.frame. So after using dplyr to produce data this is the result:

class(data)

> [1] "tbl_df"     "tbl"        "data.frame"

melt(data, id = c("X", "Y"))

>Error in match.names(clabs, names(xi)) : 
names do not match previous names

To fix this issue, I had to convert the dplyr output to a data frame. This also appears to be the recommended way to combine these packages:

data <- as.data.frame(data)
class(data)

> [1] "data.frame"

melt(data, id = c("X", "Y"))

The last block then completes without error.

Alex A.
  • 2,646
  • 22
  • 36
5

It works for me. I did the following.

library(reshape2)
tableMS <- read.table(text='     X   Y        Z        T
1  375 855 455.7259 3777.856
2  395 969 347.8306   2506.7
3  449 811 309.9512 519.8513
4  451 774  278.291 717.8705
5  453 774  278.291 717.8705
6  455 774  278.291 717.8705
7  521 697  376.734 693.8541
8  529 855 455.7259 3777.856
9  531 855 455.7259 3777.856
10 609 774  278.291 717.8705',header=TRUE)

EDIT This still work even if you coerce Z and T to a list.

tableMS$Z <- as.list(tableMS$Z)
tableMS$T <- as.list(tableMS$T)


MeltTable <- melt(tableMS,id=c("X","Y"))
# MeltTable
# X   Y variable     value
# 1  375 855        Z  455.7259
# 2  395 969        Z  347.8306
# 3  449 811        Z  309.9512
# 4  451 774        Z  278.2910
# 5  453 774        Z  278.2910
# 6  455 774        Z  278.2910
# 7  521 697        Z  376.7340
# 8  529 855        Z  455.7259
# 9  531 855        Z  455.7259
# 10 609 774        Z  278.2910
# 11 375 855        T 3777.8560
# 12 395 969        T 2506.7000
# 13 449 811        T  519.8513
# 14 451 774        T  717.8705
# 15 453 774        T  717.8705
# 16 455 774        T  717.8705
# 17 521 697        T  693.8541
# 18 529 855        T 3777.8560
# 19 531 855        T 3777.8560
# 20 609 774        T  717.8705

edit don't work with reshape2 version 1.4.2

A workaround is to use data.table package. BTW this solution is faster.

library(data.table)
tableMS$Z <- as.vector(as.list(tableMS$Z))
tableMS$T <- as.vector(as.list(tableMS$T))
setDT(tableMS)
melt(tableMS,id=c("X","Y"))
Uwe
  • 41,420
  • 11
  • 90
  • 134
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • This does not work for me, as I generate tableMS as portion of a bigger table. – Claudia Jun 05 '13 at 14:17
  • @Lilith When you copy and paste my answer it should work for you. Whatever glad that you find a solution. – agstudy Jun 05 '13 at 14:41
  • @agstudy I've tried to reproduce your answer with `reshape2` version 1.4.2 but failed with the message _Error in eval(substitute(expr), envir, enclos) : Can't melt data.frames with non-atomic 'measure' columns_ – Uwe Jan 16 '17 at 13:46
  • @agstudy That's strange. Just to make sure, I've opened a new R GUI session (R 3.3.2, Windoes 64-bit), copied your 5 statements and ended up with the same error message. It works as long as `Z`and `T` are _not_ being coerced to lists. – Uwe Jan 16 '17 at 13:59
  • @agstudy Excellent. `data.table` would have been my next try. – Uwe Jan 16 '17 at 14:14
3

try:

tableMS <- data.frame(tableMS)

Then melt tableMS the way you want.

slfan
  • 8,950
  • 115
  • 65
  • 78
Kristin
  • 113
  • 1
  • 7