# Topic 11
# probability is always between 0 and 1
#
# demonstrate a small "catch" to this, namely
# that R expresses really small, close to 0,
# values in "scientific" form. For example,
# look at the number 0.00000006478.
0.00000006478
# we will run 4 trials of the experiment of
# getting 5 random values between 1 and 99.
# Note that if you run this script then you
# will get different results than are shown
# here. Sort the values to make it easier to
# inspect the lists.
sort( as.integer( runif( 5, 1, 100)) )
sort( as.integer( runif( 5, 1, 100)) )
sort( as.integer( runif( 5, 1, 100)) )
sort( as.integer( runif( 5, 1, 100)) )
# Set up three different choices
blue_hat <- c(1,2,3,4,5,6,7,8)
coin <- c("H","T")
fruit <- c("O","P","A","G","L")
# Get thee sample space of an item
# taken from each choice
expand.grid( blue_hat, coin, fruit )
# look at permutations
# we need to load a special package to get
# the function we want.
install.packages("gtools")
library(gtools)
# recall the values in fruit
fruit
# Now that we have the library installed we
# can use the function permutations to get the
# permutations of our 5 fruits taken 3 at a time.
permutations( 5, 3, fruit )
# demonstrate factorial()
factorial(8)
factorial(6)
# look at the surprising case
factorial( 0)
# find the number of permutations of 19 things
# taken 4 at a time
factorial( 19 ) / factorial( 19-4 )
# load all of the related functions into the
# environment
source("../combinations.R")
# compute the answer to the previous problem
# using the two functions nPr() and num_perm().
nPr( 19, 4)
num_perm( 19, 4 )
# get the combinations of the 5 fruits taken
# 3 at a time
combinations( 5, 3, fruit)
# find the number of combinations of 17 things
# taken 6 at a time
factorial( 17 ) /(factorial(17-6)*factorial(6))
# do this with the functions nCr and num_comb
nCr( 17, 6 )
num_comb( 17, 6 )