sorry, i forgot to mention that you will need an extra test.... |-) tmp <- combinations(3, 3, rep=TRUE) out <- colSums(apply(tmp, 1, duplicated))+1 out[out == 1] <- 0
but now, re-reading your message, you say "(..) want to count the number of times each element appears in each arrangement (...)" apply(tmp, 1, function(v) table(factor(v, levels=1:3))) might be what you actually meant. sorry for the confusion, b On Mar 6, 2007, at 2:00 AM, Benilton Carvalho wrote: > is this what you mean? > > tmp <- combinations(3, 3, rep=TRUE) > colSums(apply(tmp, 1, duplicated))+1 > > b > > On Mar 6, 2007, at 1:16 AM, Dylan Arena wrote: > >> Hi there, >> >> >> I'm writing a function that calculates the probability of different >> outcomes of dice rolls (e.g., the sum of the highest three rolls of >> five six-sided dice). I'm using the "combinations" function from the >> "gtools" package, which is great: it gives me a matrix with all of >> the >> possible combinations (with repetitions allowed). Now I want to >> count >> the number of times each element appears in each arrangement so I can >> calculate the number of permutations of that arrangement. E.g., if I >> get output like: >> >>> combinations(3,3, rep=TRUE) >> [,1] [,2] [,3] >> [1,] 1 1 1 >> [2,] 1 1 2 >> [3,] 1 1 3 >> [4,] 1 2 2 >> [5,] 1 2 3 >> [6,] 1 3 3 >> [7,] 2 2 2 >> [8,] 2 2 3 >> [9,] 2 3 3 >> [10,] 3 3 3 >> >> I'd like to be able to determine that the first row has 3 >> repetitions, >> yielding 3!/3! = 1 permutation, while the second row has 3 >> repetitions, yielding 3!/2! = 3 permutations, etc. (This gets harder >> when there are large numbers of dice with many faces.) >> >> I know there are simple things to do, like iterating over the rows >> with for loops, but I've heard that for loops are sub-optimal in R, >> and I'd like to see what an elegant solution would look like. >> >> E.g., I might like to use sapply() with whatever function I come up >> with; I thought of using something like duplicated() and just >> counting >> the number of TRUEs that are returned for each vector (since the >> elements are always returned in non-decreasing order), but I'm >> optimistic that there is a better (faster/cleaner) way. >> >> So here is my question in a nutshell: >> Does anyone have ideas for how I might efficiently process a matrix >> like that returned by a call to combinations(n, r, rep=TRUE) to >> determine the number of repetitions of each element in each row of >> the >> matrix? If so, I'd love to hear them! >> >> >> Thanks very much for your time, >> Dylan Arena >> (Statistics M.S. student) >> >> ______________________________________________ >> R-help@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 >> and provide commented, minimal, self-contained, reproducible code. > > ______________________________________________ > R-help@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 > and provide commented, minimal, self-contained, reproducible code. ______________________________________________ R-help@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 and provide commented, minimal, self-contained, reproducible code.