[R] plyr and table question

hadley wickham h.wickham at gmail.com
Fri Apr 3 15:16:38 CEST 2009


On Fri, Apr 3, 2009 at 4:43 AM, baptiste auguie <ba208 at exeter.ac.uk> wrote:
> Dear all,
>
> I'm puzzled by the following example inspired by a recent question on
> R-help,
>
>
> cc <- textConnection("user_id  website          time
> 20        google            0930
> 21        yahoo            0935
> 20        facebook        1000
> 25        facebook        1015
> 61        google            0940")
>
> d <- read.table(cc, head=T) ; close(cc)
>
> table(d$user_id) # count the occurrences
>
> # now I'd like to include these results in the original data.frame,
>
> ddply(d, .(website), transform, count = table(user_id)) # why two new
> columns?

Because ddply expects a data frame as output from your aggregation
function.  When the output isn't a data frame, it calls as.data.frame,
which in this case produces a data frame with two columns.

Hadley

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




More information about the R-help mailing list