Re: [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 __ 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.
Re: [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: [EMAIL PROTECTED] clave-gpg: 2C260A95 __ 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] 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,]111 [2,]112 [3,]113 [4,]122 [5,]123 [6,]133 [7,]222 [8,]223 [9,]233 [10,]333 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.
Re: [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,]111 [2,]112 [3,]113 [4,]122 [5,]123 [6,]133 [7,]222 [8,]223 [9,]233 [10,]333 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.
Re: [R] Is there a quick way to count the number of times each element in a vector appears?
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,]111 [2,]112 [3,]113 [4,]122 [5,]123 [6,]133 [7,]222 [8,]223 [9,]233 [10,]333 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.