R - Reshape - Melt Error

2019-03-03 10:35发布

问题:

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: "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" ...
 $ A20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ B20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ C20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ D20    : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...

zx7.melt <- melt(zx7, id=c("calday.new"))
Error in `[<-.ts`(`*tmp*`, ri, value = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0,  : only replacement of elements is allowed

回答1:

I don't konw how did you create your structure, but when I do this , it works for me

zx7 <- data.frame( calday.new=seq(from = as.Date('2011-01-03'),by=1,length.out=519),
                   A20=ts(rep(0,519)),
                   B20=ts(rep(0,519)),
                   C20=ts(rep(0,519)),
                   D20=ts(rep(0,519)))

I create the same structure as above :

str(zx7)
'data.frame':   519 obs. of  5 variables:
 $ calday.new: Date, format: "2011-01-03" "2011-01-04" "2011-01-05" ...
 $ A20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ B20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ C20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...
 $ D20       : Time-Series  from 1 to 519: 0 0 0 0 0 0 0 0 0 0 ...

Then I melt :

head(melt(zx7, 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:

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.



标签: r reshape melt