Use R to answer the following questions: This problem will use R to find all pos
ID: 3374918 • Letter: U
Question
Use R to answer the following questions:
This problem will use R to find all possible orderings of 7 objects, and probabilities associated with them. Consider your vector of possible values to be: values = as.character(1:7)
(a) Use the function sample to draw from values 7 times (without replacement), and return this vector. Notice it is a vector, with 7 values. Display your particular draw.
(b) Repeat (a) 100000 times using an sapply. Notice the result has 7 rows, and 100000 columns, where each column is a specific random draw. Use this result to find how many of your orderings begin with the character 1.
(c) Use your samples from (b) to find the probability that a random ordering started with a 3 and ended with a 7.
(d) The function paste can collapse a vector of many characters into a single character with the following command: one.order = paste(one.draw, collapse = "") Modify the above and use it with an sapply to find how many unique orderings of 7 values there are (assuming order matters and no repetitions are allowed).
Explanation / Answer
a)
values = as.character(1:7)
s1<-sample(values)
s1 is
"7" "4" "1" "3" "6" "5" "2"
b)
t<-sapply(as.data.frame(replicate(100000,values)), sample)
#t is out dataset
> sum(t[1,]=="1")
[1] 14319
so 14319 start with 1
c)
> sum(t[1,] =='3' & t[7,]=='7')
[1] 2294
d)
one.order = apply(t,2, paste, collapse = "")
length(unique(one.order))
we get 5040 unique
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.