R - Reshape - Melt Error

后端 未结 2 1773
甜味超标
甜味超标 2021-01-27 09:50

I am trying to melt a data frame and I get this weird error. Any ideas why?

str(zx7)
\'data.frame\':   519 obs. of  5 variables:
 $ calday.new: Date, format: \"2         


        
2条回答
  •  梦毁少年i
    2021-01-27 10:04

    The problem is that the old "reshape" package's melt() function does not know what to do when it encounters an object with the class ts.

    So, you have two obvious options (though perhaps there are more):

    1. unclass the variables that are currently classed as ts before you melt() your data:

      zx7b <- zx7         # Make a backup, just in case
      library(reshape)    # Notice this is "reshape", not "reshape2"
      head(melt(zx7b, id=c("calday.new"))) # Doesn't work
      # Error in `[<-.ts`(`*tmp*`, ri, value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  : 
      #   only replacement of elements is allowed
      
      ## Unclass the relevant columns from your data.frame
      zx7b[sapply(zx7b, is.ts)] <- lapply(zx7b[sapply(zx7b, is.ts)],
                                          unclass)
      head(melt(zx7b, id=c("calday.new")))
      #   calday.new variable value
      # 1 2011-01-03      A20     0
      # 2 2011-01-04      A20     0
      # 3 2011-01-05      A20     0
      # 4 2011-01-06      A20     0
      # 5 2011-01-07      A20     0
      # 6 2011-01-08      A20     0
      
    2. Upgrade to "reshape2" instead, and no unclassing is required.

      library(reshape2) # Notice that this is reshape2!
      head(melt(zx7, id=c("calday.new"))) # Melt the original data.frame
      #   calday.new variable value
      # 1 2011-01-03      A20     0
      # 2 2011-01-04      A20     0
      # 3 2011-01-05      A20     0
      # 4 2011-01-06      A20     0
      # 5 2011-01-07      A20     0
      # 6 2011-01-08      A20     0
      

    I haven't taken the time to do it, but you can check the code for the melt.data.frame methods for melt() from each version of the "reshape" packages and see where the differences lie. Install both packages and then type reshape2:::melt.data.frame and reshape:::melt.data.frame to see the underlying functions.

提交回复
热议问题