Daniel Brewer
2010-Dec-10 14:47 UTC
[R] melt causes errors when characters and values are used
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}}
Dennis Murphy
2010-Dec-11 11:20 UTC
[R] melt causes errors when characters and values are used
Hi: On Fri, Dec 10, 2010 at 6:47 AM, Daniel Brewer <daniel.brewer@icr.ac.uk>wrote:> 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, >In this case, thankfully so - it's warning that you're trying to mix numeric and factor variables.> > 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? >For this question, convert value to character before melting it.> > 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. >This is a more effective way, taken from a post on the ggplot2 list, courtesy of Kohske Takahashi: # Toy data: df <- data.frame(id = as.character(101:110), date1 = seq(from = as.Date('2006-01-10'), to = as.Date('2006-01-19'), by = 'day'), value1 = rpois(10, 5), date2 = seq(from = as.Date('2006-02-10'), to = as.Date('2006-02-19'), by = 'day'), value2 = rpois(10, 10), stringsAsFactors = FALSE) dfm <- data.frame( melt(df, id = 'id', measure = c(grep('^date', names(df)))), myval = melt(df, id = 'id', measure = c(grep('^val', names(df))))$value ) names(dfm)[3] <- 'date' The first melt() call creates the three variables that are normally returned from melt(): id, variable and value. The second, internal melt() call creates a stacked column for the values without creating a new 'variable'. One can repeat the game with multiple sets of variables. Of course, there is an alternative way to reshape such data with the reshape() function... HTH, Dennis Thanks> > Dan > -- > ************************************************************** > Daniel Brewer, Ph.D. > > Institute of Cancer Research > Molecular Carcinogenesis > Email: daniel.brewer@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...{{dropped:13}}