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
I don't konw how did you create your structure, but when I do this , it works for me
I create the same structure as above :
Then I melt :
The problem is that the old "reshape" package's
melt()
function does not know what to do when it encounters an object with the classts
.So, you have two obvious options (though perhaps there are more):
unclass
the variables that are currently classed asts
before youmelt()
your data:Upgrade to "reshape2" instead, and no unclassing is required.
I haven't taken the time to do it, but you can check the code for the
melt.data.frame
methods formelt()
from each version of the "reshape" packages and see where the differences lie. Install both packages and then typereshape2:::melt.data.frame
andreshape:::melt.data.frame
to see the underlying functions.