Skip to content
Back to formatted view

Raw Message

Message-ID: <f8e6ff050904061053k264e4546t177400073aeea0bf@mail.gmail.com>
Date: 2009-04-06T17:53:48Z
From: Hadley Wickham
Subject: Collapse data matrix with extra info separated by commas
In-Reply-To: <EC032524-0DEA-4B28-9084-88BFDB23AF96@exeter.ac.uk>

On Mon, Apr 6, 2009 at 10:40 AM, baptiste auguie <ba208 at exeter.ac.uk> wrote:
> Here's one attempt with plyr, hopefully Hadley will give you a better
> solution ( I could not get cast() to do it either)
>
> test <-
> data.frame(a=c("A","A","A","A","B","B","B"),b=c(1,1,2,2,1,1,1),c=sample(1:7))
> ddply(test,.(a,b),.fun=function(.) paste(.)[3])

This is a problem that currently isn't very easy to solve in plyr (but
I'm working on it).  About the best you can do is:

ddply(test, ~ a + b, colwise(paste, .(c)), collapse =",")

(this is basically equivalent to your suggestion)

Hadley


-- 
http://had.co.nz/