Dear list, I'm trying to use the reshape package to perform a merging operation on a list of data.frames as illustrated below,> a <- 1:10 > example <- list( data.frame(a=a, b=sin(a)), data.frame(a=a, > b=cos(a)) ) > > melt(example, id = a)this produces the desired result, where the data.frames have been coerced into one with a common identifier variable "a". However, it seems that if "a" is of mode numeric it is not recognized as an id variable,> a <- as.numeric(1:10) > > example <- list(data.frame(a=a, b=sin(a)), data.frame(a=a, b=cos(a))) > > melt(example, id = a) # this does not use a as an id variableI'm very new to the reshape package, any pointer would be greatly appreciated. I first tried several combinations of merge, do.call, sapply,... but without success. Many thanks, baptiste _____________________________ Baptiste Augui? School of Physics University of Exeter Stocker Road, Exeter, Devon, EX4 4QL, UK Phone: +44 1392 264187 http://newton.ex.ac.uk/research/emag
baptiste auguie <ba208 <at> exeter.ac.uk> writes:> > I'm trying to use the reshape package to perform a merging operation > on a list of data.frames as illustrated below, > > > a <- 1:10 > > example <- list( data.frame(a=a, b=sin(a)), data.frame(a=a, > > b=cos(a)) ) > > > > melt(example, id = a) > > this produces the desired result, where the data.frames have been > coerced into one with a common identifier variable "a". However, it > seems that if "a" is of mode numeric it is not recognized as an id > variable, > > > a <- as.numeric(1:10) > > > > example <- list(data.frame(a=a, b=sin(a)), data.frame(a=a, b=cos(a))) > > > > melt(example, id = a) # this does not use a as an id variable >This is the documented behavior: Only integers and factors are used for grouping, but as.numeric is double, even if 1:10 looks like integers a <- as.integer(1:10) or simply a <- 1:10 Dieter
Given that I cannot arbitrarily change the data to make "a" an integer, can I still use "a" as a grouping variable? I tried melt(example, id = factor(a)) but it does not work either. Must this change from numeric values to factors be done before applying melt? Thanks, baptiste On 25 Jul 2008, at 16:35, Dieter Menne wrote:>> >> >>> a <- as.numeric(1:10) >>> >>> example <- list(data.frame(a=a, b=sin(a)), data.frame(a=a, >>> b=cos(a))) >>> >>> melt(example, id = a) # this does not use a as an id variable >> > > This is the documented behavior: Only integers and factors are used > for > grouping, but as.numeric is double, even if 1:10 looks like integers > > a <- as.integer(1:10) > or simply > > a <- 1:10 > > Dieter
On Fri, Jul 25, 2008 at 9:49 AM, baptiste auguie <ba208 at exeter.ac.uk> wrote:> Dear list, > > > I'm trying to use the reshape package to perform a merging operation on a > list of data.frames as illustrated below, > >> a <- 1:10 >> example <- list( data.frame(a=a, b=sin(a)), data.frame(a=a, b=cos(a)) ) >> >> melt(example, id = a)You want: melt(example, id = "a") i.e. the id argument is a character or numeric vector specifying which variables to use as id variables. Your call would be equivalent to melt(example, id = 1:10) which clearly is incorrect for your example. Hadley -- http://had.co.nz/