[R] Need help to transform data into co-occurence matix

Liaw, Andy andy_liaw at merck.com
Fri Jan 21 04:41:24 CET 2005


> From: Judie Z
> 
> Dear R experts,
> I have the data in the following fomat(from some kind of card 
> sorting process)
>  
> ID  Category   Card numbers
> 1   1               1,2,5
> 1   2               3,4
> 2   1               1,2
> 2   2               3
> 2   3               4,5
>  
> I want to transform this data into two co-occurence matrix 
> (one for each ID)
> -- For ID 1
>    1 2 3 4 5
> 1 1 1 0 0 1
> 2 1 1 0 0 1
> 3 0 0 1 1 0
> 4 0 0 1 1 0
> 5 1 1 0 0 1
>  
> -- For ID 2
>    1 2 3 4 5
> 1  1 1 0 0 0
> 2  1 1 0 0 0
> 3  0 0 1 0 0 
> 4  0 0 0 1 1
> 5  0 0 0 1 1
>  
> The columns and rows are representing the card numbers. All 
> "0"s mean the card numbers are not in the same category, vice versa.
>  
> Is there any way I can to this in R?
> I would really appreciate your help. 

It depends on how the data are structured in R.  Here's an example (I'm sure
others can come up with more clever/efficient ways):

> cardlist <- list(c(1,2,5), c(3,4))
> indicator <- function(i, n=max(i)) { x <- rep(0, n); x[i] <- 1; x}
> matrix(rowSums(sapply(cardlist, function(i) crossprod(t(indicator(i,
5))))), nrow=5)
     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    0    0    1
[2,]    1    1    0    0    1
[3,]    0    0    1    1    0
[4,]    0    0    1    1    0
[5,]    1    1    0    0    1

which is the matrix for ID 1 in your example.

HTH,
Andy
  
> Judie, Tie
> 		
> ---------------------------------
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 
>




More information about the R-help mailing list