Hi all, I need to compute some "occurence matrix": given a zero matrix and a set of paired indexes, I want to store the number of occurences of each paired index in a matrix. The paired indexes are stores as an index matrix. I prefere not to use loops for performances purpose. Here follows a dummy example:> occurence <- matrix(0, 2, 2); data[,1] [,2] [1,] 0 0 [2,] 0 0> > index <- matrix(1, 3, 2); index[,1] [,2] [1,] 1 1 [2,] 1 1 [3,] 1 1> > occurence[index] <- occurence[index] + 1I was expecting the folowing result:> occurence[,1] [,2] [1,] 3 0 [2,] 0 0 I get instead:> occurence[,1] [,2] [1,] 1 0 [2,] 0 0 I guess that there is some "hidden copy" involved but I wanted to know if there is an efficient workaround (not using some loop structure). I thought "factors" could do the job but I didn't manage to use them for that problem. ---------------------------------------------------------------------------- Pascal BLEUYARD Laboratoire de M?t?orologie Physique (LaMP) OPGC, Universit? Blaise Pascal 24, avenue des Landais 63177 AUBIERE CEDEX T?l : 04 73 40 73 75 Fax : 04 73 40 51 36 M?l : P.Bleuyard at opgc.univ-bpclermont.fr
Hi Pascal, One thing you can do is to work on indexes, count unique occurences and assign them, as following:> index <- as.data.frame(table(index[,1],index[,2])) > index <- do.call("cbind",lapply(index, as.numeric)) # ensures numericcoding (as table turns into factors)> occurence[index[,1:2]] <- index[,3] > occurence[,1] [,2] [1,] 3 0 [2,] 0 0 Eric Eric Lecoutre UCL / Institut de Statistique Voie du Roman Pays, 20 1348 Louvain-la-Neuve Belgium tel: (+32)(0)10473050 lecoutre at stat.ucl.ac.be http://www.stat.ucl.ac.be/ISpersonnel/lecoutre If the statistics are boring, then you've got the wrong numbers. -Edward Tufte> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Pascal BLEUYARD > Sent: mardi 22 mars 2005 11:26 > To: r-help at stat.math.ethz.ch > Subject: [R] Newbie: Matrix indexing > > > Hi all, > > I need to compute some "occurence matrix": given a zero > matrix and a set of paired indexes, I want to store the > number of occurences of each paired index in a matrix. The > paired indexes are stores as an index matrix. I prefere not > to use loops for performances purpose. > > Here follows a dummy example: > > > occurence <- matrix(0, 2, 2); data > [,1] [,2] > [1,] 0 0 > [2,] 0 0 > > > > index <- matrix(1, 3, 2); index > [,1] [,2] > [1,] 1 1 > [2,] 1 1 > [3,] 1 1 > > > > occurence[index] <- occurence[index] + 1 > > I was expecting the folowing result: > > > occurence > [,1] [,2] > [1,] 3 0 > [2,] 0 0 > > I get instead: > > > occurence > [,1] [,2] > [1,] 1 0 > [2,] 0 0 > > I guess that there is some "hidden copy" involved but I > wanted to know if there is an efficient workaround (not using > some loop structure). I thought "factors" could do the job > but I didn't manage to use them for that problem. > > -------------------------------------------------------------- > -------------- > Pascal BLEUYARD > Laboratoire de M?t?orologie Physique (LaMP) > OPGC, Universit? Blaise Pascal > 24, avenue des Landais > 63177 AUBIERE CEDEX > T?l : 04 73 40 73 75 > Fax : 04 73 40 51 36 > M?l : P.Bleuyard at opgc.univ-bpclermont.fr > > ______________________________________________ > 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 >
It would be a lot easier if try to do the reverse by generating the table last. However you will need to coerce your numeric data into factors first. See what happens with the first example when you omit the factor coercion. mat <- matrix(1, nr=3, nc2) # called 'index' in your example my.levels <- c(1, 2) table( factor( mat[ ,1], levels=my.levels), factor( mat[ ,2], levels=my.levels) ) 1 2 1 3 0 2 0 0 Here is a slightly more interesting examples. mat <- matrix(c(1,1, 1,1, 1,2, 2,2, 1,2) , nr=5, nc=2, byrow=T) my.levels <- c(1, 2, 3) table( factor(mat[ ,1], levels=my.levels), factor(mat[ ,2], levels=my.levels) ) 1 2 3 1 2 2 0 2 0 1 0 3 0 0 0 Regards, Adai On Tue, 2005-03-22 at 11:26 +0100, Pascal BLEUYARD wrote:> Hi all, > > I need to compute some "occurence matrix": given a zero matrix and a set > of paired indexes, I want to store the number of occurences of each paired > index in a matrix. The paired indexes are stores as an index matrix. I > prefere not to use loops for performances purpose. > > Here follows a dummy example: > > > occurence <- matrix(0, 2, 2); data > [,1] [,2] > [1,] 0 0 > [2,] 0 0 > > > > index <- matrix(1, 3, 2); index > [,1] [,2] > [1,] 1 1 > [2,] 1 1 > [3,] 1 1 > > > > occurence[index] <- occurence[index] + 1 > > I was expecting the folowing result: > > > occurence > [,1] [,2] > [1,] 3 0 > [2,] 0 0 > > I get instead: > > > occurence > [,1] [,2] > [1,] 1 0 > [2,] 0 0 > > I guess that there is some "hidden copy" involved but I wanted to know if > there is an efficient workaround (not using some loop structure). I thought > "factors" could do the job but I didn't manage to use them for that problem. > > ---------------------------------------------------------------------------- > Pascal BLEUYARD > Laboratoire de M?t?orologie Physique (LaMP) > OPGC, Universit? Blaise Pascal > 24, avenue des Landais > 63177 AUBIERE CEDEX > T?l : 04 73 40 73 75 > Fax : 04 73 40 51 36 > M?l : P.Bleuyard at opgc.univ-bpclermont.fr > > ______________________________________________ > 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 >
Pascal BLEUYARD <p.bleuyard <at> opgc.univ-bpclermont.fr> writes: : : Hi all, : : I need to compute some "occurence matrix": given a zero matrix and a set : of paired indexes, I want to store the number of occurences of each paired : index in a matrix. The paired indexes are stores as an index matrix. I : prefere not to use loops for performances purpose. : : Here follows a dummy example: : : > occurence <- matrix(0, 2, 2); data : [,1] [,2] : [1,] 0 0 : [2,] 0 0 : > : > index <- matrix(1, 3, 2); index : [,1] [,2] : [1,] 1 1 : [2,] 1 1 : [3,] 1 1 : > : > occurence[index] <- occurence[index] + 1 : : I was expecting the folowing result: : : > occurence : [,1] [,2] : [1,] 3 0 : [2,] 0 0 : : I get instead: : : > occurence : [,1] [,2] : [1,] 1 0 : [2,] 0 0 : : I guess that there is some "hidden copy" involved but I wanted to know if : there is an efficient workaround (not using some loop structure). I thought : "factors" could do the job but I didn't manage to use them for that problem. Turn your index matrix into a data frame so you can use lapply on it. Then convert each of the two columns to a two-level factor. Now you can use table on the result: table(lapply(as.data.frame(index), factor, lev = 1:2))