[R] avoid a loop

David Winsemius dwinsemius at comcast.net
Thu Nov 4 21:27:46 CET 2010


On Nov 4, 2010, at 4:24 PM, Sarah Goslee wrote:

> Here's one possibility:
>
>> library(ecodist)
>> a <- c(1,1,1,2,2,3,3,3,3)
>> b <- c("a","b","c","a","d","a", "b", "e", "f")
>>
>> x <- crosstab(a, b, rep(1, length(a)))
>> x
>  a b c d e f
> 1 1 1 1 0 0 0
> 2 1 0 0 1 0 0
> 3 1 1 0 0 1 1
>> x %*% t(x)
>  1 2 3
> 1 3 1 2
> 2 1 2 1
> 3 2 1 4

Antoher way:

 > sapply(1:3, function(y) {
              sapply(1:3,  function(x){
                          length(intersect(b[a==y], b[a==x]) )
  } ) } )

      [,1] [,2] [,3]
[1,]    3    1    2
[2,]    1    2    1
[3,]    2    1    4


>
> Sarah
>
> On Thu, Nov 4, 2010 at 3:42 PM, cory n <corynissen at 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
>>
>
>
>
> -- 
> Sarah Goslee
> http://www.functionaldiversity.org
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list