Hi, R function aggregate can only take summary stats functions, can I aggregate text columns? For example, for the dataframe below,> a <- rbind(data.frame(id=1, name='Tom',hobby='fishing'),data.frame(id=1, name='Tom', hobby='reading'),data.frame(id=2, name='Mary', hobby='reading'),data.frame(id=3, name='John', hobby='boating'),data.frame(id=2, name='Mary', hobby='running'))> aid name hobby 1 1 Tom fishing 2 1 Tom reading 3 2 Mary reading 4 3 John boating 5 2 Mary running I want output as>bid name hobbies 1 Tom fishing reading 2 Mary reading running 3 John boating Thanks, Richard [[alternative HTML version deleted]]
try this using sqldf:> aid name hobby 1 1 Tom fishing 2 1 Tom reading 3 2 Mary reading 4 3 John boating 5 2 Mary running> require(sqldf) > sqldf('select name, group_concat(hobby) hobby from a group by id', method='raw')name hobby 1 Tom fishing,reading 2 Mary reading,running 3 John boating On Thu, Oct 7, 2010 at 11:52 AM, Tan, Richard <RTan at panagora.com> wrote:> Hi, R function aggregate can only take summary stats functions, can I > aggregate text columns? ?For example, for the dataframe below, > > > >> a <- rbind(data.frame(id=1, name='Tom', > hobby='fishing'),data.frame(id=1, name='Tom', > hobby='reading'),data.frame(id=2, name='Mary', > hobby='reading'),data.frame(id=3, name='John', > hobby='boating'),data.frame(id=2, name='Mary', hobby='running')) > >> a > > ?id name ? hobby > > 1 ?1 ?Tom fishing > > 2 ?1 ?Tom reading > > 3 ?2 Mary reading > > 4 ?3 John boating > > 5 ?2 Mary running > > > > > > I want output as > >>b > > id name hobbies > > 1 Tom ? ?fishing reading > > 2 Mary reading running > > 3 John boating > > > > > > > > Thanks, > > Richard > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Try this: aggregate(hobby ~ id + name, a, FUN = toString) On Thu, Oct 7, 2010 at 12:52 PM, Tan, Richard <RTan@panagora.com> wrote:> Hi, R function aggregate can only take summary stats functions, can I > aggregate text columns? For example, for the dataframe below, > > > > > a <- rbind(data.frame(id=1, name='Tom', > hobby='fishing'),data.frame(id=1, name='Tom', > hobby='reading'),data.frame(id=2, name='Mary', > hobby='reading'),data.frame(id=3, name='John', > hobby='boating'),data.frame(id=2, name='Mary', hobby='running')) > > > a > > id name hobby > > 1 1 Tom fishing > > 2 1 Tom reading > > 3 2 Mary reading > > 4 3 John boating > > 5 2 Mary running > > > > > > I want output as > > >b > > id name hobbies > > 1 Tom fishing reading > > 2 Mary reading running > > 3 John boating > > > > > > > > Thanks, > > Richard > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
Richard - Yes, you certainly can use aggregate to acheive what you want:> aggregate(a$hobby,a['name'],paste,collapse=' ')name x 1 Tom fishing reading 2 Mary reading running 3 John boating - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Thu, 7 Oct 2010, Tan, Richard wrote:> Hi, R function aggregate can only take summary stats functions, can I > aggregate text columns? For example, for the dataframe below, > > > >> a <- rbind(data.frame(id=1, name='Tom', > hobby='fishing'),data.frame(id=1, name='Tom', > hobby='reading'),data.frame(id=2, name='Mary', > hobby='reading'),data.frame(id=3, name='John', > hobby='boating'),data.frame(id=2, name='Mary', hobby='running')) > >> a > > id name hobby > > 1 1 Tom fishing > > 2 1 Tom reading > > 3 2 Mary reading > > 4 3 John boating > > 5 2 Mary running > > > > > > I want output as > >> b > > id name hobbies > > 1 Tom fishing reading > > 2 Mary reading running > > 3 John boating > > > > > > > > Thanks, > > Richard > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >