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',
try this using sqldf:
a
id 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
Or:
data.frame( hobs= tapply(a$hobby, list( a$name), c))
hobs
Tom fishing, reading
Mary reading, running
John boating
Note Jim's gives you the names as columns while this has them as
rownames. Further differences : my version has the column as lists
whereas Jim's
Try this:
aggregate(hobby ~ id + name, a, FUN = toString)
On Thu, Oct 7, 2010 at 12:52 PM, Tan, Richard r...@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 -
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
Thank you!
Richard
-Original Message-
From: jim holtman [mailto:jholt...@gmail.com]
Sent: Thursday, October 07, 2010 12:08 PM
To: Tan, Richard
Cc: r-help@r-project.org
Subject: Re: [R] aggregate text column by a few rows
try this using sqldf:
a
id name hobby
1 1 Tom fishing
2
6 matches
Mail list logo