Skip to content

Instantly share code, notes, and snippets.

@davetang
Created September 8, 2013 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davetang/6485079 to your computer and use it in GitHub Desktop.
Save davetang/6485079 to your computer and use it in GitHub Desktop.
Calculating the number of permutations with repetition/replacement
#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