Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R cannot melt data.frame

Tags:

r

melt

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
like image 938
Claudia Avatar asked Jun 05 '13 13:06

Claudia


People also ask

What package in R has melt?

The melt function is to be found in the reshape package. If you do not have that package installed, then you will need to install it with install. packages("reshape") before you can use it. Then, when the package is installed, make it available with library(reshape) .

What is melt () in R?

The melt() function in R programming is an in-built function. It enables us to reshape and elongate the data frames in a user-defined manner. It organizes the data values in a long data frame format.

What is the opposite of melt in R?

5. 4. In package reshape2 the opposite of melt is cast.

What is melt and cast in R?

melt() and cast() are the functions that efficiently reshape the data. There are many packages in R that require data reshaping. Each data is specified in multiple rows of dataframe with different details in each row and this type of format of data is known as long format.


1 Answers

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.

like image 53
Alex A. Avatar answered Oct 05 '22 08:10

Alex A.