R cannot melt data.frame

前端 未结 3 2024
青春惊慌失措
青春惊慌失措 2020-12-30 05:55

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 5         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-30 06:37

    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.

提交回复
热议问题