Hi:

To mimic Sarah Goslee's reply within base R, either of these work:

crossprod(t(as.matrix(xtabs( ~ a + b))))
crossprod(t(as.matrix(table(a, b))))

HTH,
Dennis

On Thu, Nov 4, 2010 at 12:42 PM, cory n <corynis...@gmail.com> wrote:

> Let's suppose I have userids and associated attributes...  columns a and b
>
> a <- c(1,1,1,2,2,3,3,3,3)
> b <- c("a","b","c","a","d","a", "b", "e", "f")
>
> so a unique list of a would be
>
> id <- unique(a)
>
> I want a matrix like this...
>
>     [,1] [,2] [,3]
> [1,]    3    1    2
> [2,]    1    2    1
> [3,]    2    1    4
>
> Where element i,j is the number of items in b that id[i] and id[j] share...
>
> So for example, in element [1,3] of the result matrix, I want to see
> 2.  That is, id's 1 and 3 share two common elements in b, namely "a"
> and "b".
>
> This is hard to articulate, so sorry for the terrible description
> here.  The way I have solved it is to do a double loop, looping over
> every member of the id column and comparing it to every other member
> of id to see how many elements of b they share.  This takes forever.
>
> Thanks
>
> cn
>
> ______________________________________________
> 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.
>

        [[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.

Reply via email to