tonicebrian
tonicebrian

Reputation: 4795

Plyr for simple group-by

I'm getting data from a MySQL table that has 2 columns (idDoc, tag) describing that the document has a given tag. When I use the data frame with

ddply(tags,1)

My objective is to group tags by id, so say I do the following steps

> x=c(1,1,2,2)
> y=c(4,5,6,7)
> data.frame(x,y)
  x y
1 1 4
2 1 5
3 2 6
4 2 7

My desired output would be perhaps a list of lists (or whatever other result) that would get

 1 -> c(4,5)
 2 -> c(6,7)

Regards

Upvotes: 1

Views: 532

Answers (1)

joran
joran

Reputation: 173577

This is kind of a shot in the dark, since when you say you want an 'association', that doesn't really precisely describe any particular R data structure, so it's unclear what form you want the output to take.

But one base R possibility would be to simply use split:

split(tags$tag, tags$idDoc)

which should returned a named list where the names come from idDoc and each list element is the tags associated with that idDoc value. There will be duplicates, though. So maybe this would work better:

tapply(tags$tag,tags$idDoc,FUN = unique)

which should return a list of unique tags for each idDoc.

(Edited: No need for the anonymous function; only need to pass unique).

Upvotes: 2

Related Questions