[Rd] Combinations and Permutations
Good day, In utils, there is a function named combn. It would seem complementary for utils to also offer permutations because of how closely mathematically related they are to each other. Could permutations be added to save on a package dependency if developing a package? -- Dario Strbenac University of Sydney Camperdown NSW 2050 Australia __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Combinations and Permutations
Le 13/01/2023 à 09:00, Dario Strbenac via R-devel a écrit : Good day, In utils, there is a function named combn. It would seem complementary for utils to also offer permutations because of how closely mathematically related they are to each other. Could permutations be added to save on a package dependency if developing a package? If you need a function returning a matrix with all permutations of a vector x in its columns, a simple recursive one-liner can be sufficient, no need for a whole package dependency for this: perm=function(x) {n=length(x); f=factorial(n); if (n>1) structure(vapply(seq_along(x), function(i) rbind(x[i], perm(x[-i])), x[rep(1L, f)]), dim=c(n, f)) else x} It works for all king of vectors (integer, numeric, character, ...): perm(1:3) perm(pi*1:3) perm(letters[1:3]) Obviously, a particular attention should be brought to the size of x (referred here as n) as the column number in the returned matrix growths as n!.. E.g. 8!=40320. So growths the cpu time too. Hoping it helps, Serguei. -- Dario Strbenac University of Sydney Camperdown NSW 2050 Australia __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
Re: [Rd] Combinations and Permutations
On 1/13/23 4:11 AM, Serguei Sokol via R-devel wrote: Le 13/01/2023 à 09:00, Dario Strbenac via R-devel a écrit : Good day, In utils, there is a function named combn. It would seem complementary for utils to also offer permutations because of how closely mathematically related they are to each other. Could permutations be added to save on a package dependency if developing a package? If you need a function returning a matrix with all permutations of a vector x in its columns, a simple recursive one-liner can be sufficient, no need for a whole package dependency for this: perm=function(x) {n=length(x); f=factorial(n); if (n>1) structure(vapply(seq_along(x), function(i) rbind(x[i], perm(x[-i])), x[rep(1L, f)]), dim=c(n, f)) else x} It works for all king of vectors (integer, numeric, character, ...): perm(1:3) perm(pi*1:3) perm(letters[1:3]) Obviously, a particular attention should be brought to the size of x (referred here as n) as the column number in the returned matrix growths as n!.. E.g. 8!=40320. So growths the cpu time too. What about "combinat::permn"? Spencer Graves Hoping it helps, Serguei. -- Dario Strbenac University of Sydney Camperdown NSW 2050 Australia __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel __ R-devel@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel