Re: [R] melting a list: basic question

From: baptiste auguie <ba208_at_exeter.ac.uk>
Date: Sat, 26 Jul 2008 10:43:15 +0100

On 26 Jul 2008, at 02:52, hadley wickham wrote:

> On Fri, Jul 25, 2008 at 8:50 PM, hadley wickham
> <h.wickham_at_gmail.com> wrote:

>> 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.


Sorry about the poor example, I hadn't realized it worked only thanks to the default behavior.

> I've just noticed that there's also a bug in the released version
> (fixed in my development version) which means that the id argument to
> melt.list() is not being passed on to the individual
> melt.data.frame()s
>

Considering the following example, this makes sense:

> 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
>

> melt(example[[1]], id = "a") # the method for the individual
> data.frame works fine

Is it possible to install your development version of reshape? I could not find it alongside of ggplot2 on github. If not, I've added "..." in the method for the current version and it seems to work for me.

> melt.list <- function(data, ..., level=1) {
> var <- nulldefault(attr(data, "varname"), paste("L", level, sep=""))
> names <- nulldefault(names(data), 1:length(data))
> parts <- lapply(data, melt, level=level+1, ...)
>
> namedparts <- mapply(function(x, name) {
> x[[var]] <- name
> x
> }, parts, names, SIMPLIFY=FALSE)
> do.call(rbind.fill, namedparts)
> }

Many thanks,

baptiste



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sat 26 Jul 2008 - 09:49:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sun 27 Jul 2008 - 20:34:15 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive