Created
September 8, 2013 14:23
-
-
Save davetang/6485079 to your computer and use it in GitHub Desktop.
Calculating the number of permutations with repetition/replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#install if necessary | |
install.packages('gtools') | |
#load library | |
library(gtools) | |
#urn with 3 balls | |
x <- c('red', 'blue', 'black') | |
#pick 2 balls from the urn with replacement | |
#get all permutations | |
permutations(n=3,r=2,v=x,repeats.allowed=T) | |
# [,1] [,2] | |
# [1,] "black" "black" | |
# [2,] "black" "blue" | |
# [3,] "black" "red" | |
# [4,] "blue" "black" | |
# [5,] "blue" "blue" | |
# [6,] "blue" "red" | |
# [7,] "red" "black" | |
# [8,] "red" "blue" | |
# [9,] "red" "red" | |
#number of permutations | |
nrow(permutations(n=3,r=2,v=x,repeats.allowed=T)) | |
#[1] 9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment