Dylan Arena
2007-Mar-06 06:16 UTC
[R] Is there a quick way to count the number of times each element in a vector appears?
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)
Benilton Carvalho
2007-Mar-06 07:00 UTC
[R] Is there a quick way to count the number of times each element in a vector appears?
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 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 > and provide commented, minimal, self-contained, reproducible code.
Alberto Monteiro
2007-Mar-06 11:13 UTC
[R] Is there a quick way to count the number of times each element in a vector appears?
Dylan Arena wrote:> > 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). >You know there are simpler ways to do this, don't you? Alberto Monteiro
José Rafael Ferrer Paris
2007-Mar-06 13:23 UTC
[R] Is there a quick way to count the number of times each element in a vector appears?
El lun, 05-03-2007 a las 22:16 -0800, Dylan Arena escribi?:> 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! > >here is an answer in a nutshell: my.table <- combinations(3,3,rep=TRUE) ## one possibility is apply(my.table,1,table) ## or better, in plain table(my.table,row(my.table)) look at the help pages of ?table ?apply ?row> Thanks very much for your time, > Dylan Arena > (Statistics M.S. student) >that took probably one minute of my time... so never mind -- Dipl.-Biol. JR Ferrer Paris ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Laboratorio de Biolog?a de Organismos --- Centro de Ecolog?a Instituto Venezolano de Investigaciones Cient?ficas (IVIC) Apdo. 21827, Caracas 1020-A Rep?blica Bolivariana de Venezuela Tel: (+58-212) 504-1452 Fax: (+58-212) 504-1088 email: jferrer at ivic.ve clave-gpg: 2C260A95
Apparently Analagous Threads
- How to subset a data frame to include only first events
- Trouble with R CMD check: I can't seem to get dependencies right (maybe I'm using R_LIBS incorrectly?)
- Is there a facility in R similar to MatLab "syms" that allows using unevaluated numeric symbols in matrices?
- Polymorphic Associations
- [LLVMdev] Contributing a buildbot for the BPF backend