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. Judie, Tie --------------------------------- [[alternative HTML version deleted]]
> 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 > >
Judie, You may want to see if the MedlineR library, which has a program for constructing co-occurrence matrices, will work for you. The program can be found at: http://dbsr.duke.edu/pub/MedlineR/ Have fun with it, Tim Liao Professor of Sociology & Statistics University of Illinois Urbana, IL 61801 ---- Original message ---->Date: Thu, 20 Jan 2005 18:19:19 -0800 (PST) >From: Judie Z <judielife at yahoo.com> >Subject: [R] Need help to transform data into co-occurencematix>To: r-help at stat.math.ethz.ch > >Dear R experts, >I have the data in the following fomat(from some kind of cardsorting 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. > >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