Skip to content

melt causes errors when characters and values are used

2 messages · Daniel Brewer, Dennis Murphy

#
Hello,

I am finding that the melt function from the reshape library causes
errors when applied to a data.frame that contains numeric and character
columns.  For example,

melt(id.vars="ID",data.frame(ID=1:3,date=c("a","b","c"),value=c(1,4,5)))
  ID variable value
1  1     date     a
2  2     date     b
3  3     date     c
4  1    value  <NA>
5  2    value  <NA>
6  3    value  <NA>
Warning message:
In `[<-.factor`(`*tmp*`, ri, value = c(1, 4, 5)) :
  invalid factor level, NAs generated

It would be useful in this situation that the numerical column got
converted to a character column in this situation.  Any ways round this?

In actual fact I have got a situation where it is more like this

ID	Date_1	Value_1	Date_2	Value_2 ...

and I would like to convert it to a data.frame of ID, Date & Value but I
thought the above would be an appropriate middle step.

Thanks

Dan