[R] Creating a co-occurence matrix

Liaw, Andy andy_liaw at merck.com
Tue Jul 1 13:57:56 CEST 2003


> From: Greg Blevins [mailto:gblevins at mn.rr.com] 
> 
> Hello R experts,
> 
> I have a data.frame which has a series of 30 variables that 
> are each coded 1,0--1 if a behavior is engaged in, 0 otherwise.
> 
> The data.frame looks like this:
> 
>                         var1 var2 ...var30
> Respondent 1
> Respondent 2
> etc
> 
> I would like to create a matrix (or at least a half-matrix) 
> as follows:
> 
>               var1  var2 ...var30
> var1
> var 2
> .
> .
> var 30
> 
> where the number at each intersection is the number of people 
> who engaged in both behaviors.  I could run a whole bunch of 
> tables and input the data by hand into a new matrix, but I 
> was wondering if there is a way to do this via a 
> program/function (I looked at "daisy," but concluded this 
> function would not handle this).

If I understood you correctly, the cross product of the matrix should do it.
If your data frame is named "df", then you can do

  answer <- crossprod(data.matrix(df))

Hth,
Andy

 
> Any help would be appreciated!
> 
> Greg Blevins
> The Market Solutions Group
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
> 

------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments, ...{{dropped}}




More information about the R-help mailing list