[R] melt causes errors when characters and values are used

Daniel Brewer daniel.brewer at icr.ac.uk
Fri Dec 10 15:47:24 CET 2010


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
-- 
**************************************************************
Daniel Brewer, Ph.D.

Institute of Cancer Research
Molecular Carcinogenesis
Email: daniel.brewer at icr.ac.uk
**************************************************************

The Institute of Cancer Research: Royal Cancer Hospital, a charitable Company Limited by Guarantee, Registered in England under Company No. 534147 with its Registered Office at 123 Old Brompton Road, London SW7 3RP.

This e-mail message is confidential and for use by the a...{{dropped:2}}



More information about the R-help mailing list