In the code chunk below:
even that holds the even numbers from 1
to 100all() function and %% (modulo)
operator, confirm that all of the numbers in your even
vector are eveneven <- seq(2, 100, by=2)
even
## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
## [20] 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76
## [39] 78 80 82 84 86 88 90 92 94 96 98 100
all_are_even <- all(even %% 2 ==0)
print(all_are_even)
## [1] TRUE
In the code chunk below:
phone_numbers that contains the numbers
2, 8, 1, 3, 3, 0, 8, 0, 0, 4prefix that has the first three
elements of phone_numberssmall that has the values of
phone_numbers that are less than or equal to 5large that has the values of
phone_numbers that are strictly greater than 5phone_numbers that are larger
than 5 with the number 5phone_numbers with
the number 0For question 4, please don’t just do 2,8,1, use indexing! For questions 5 through 8, you should use not use “hard coding” for the indexes.
phone_numbers <- c(2,8,1,3,3,0,8,0,0,4)
prefix <- phone_numbers[1:3]
small <- phone_numbers[phone_numbers<=5]
small
## [1] 2 1 3 3 0 0 0 4
large <- phone_numbers[phone_numbers>5]
large
## [1] 8 8
phone_numbers[phone_numbers>5] <- 5
phone_numbers
## [1] 2 5 1 3 3 0 5 0 0 4
odd_index <- (phone_numbers %%2) != 0
odd_index
## [1] FALSE TRUE TRUE TRUE TRUE FALSE TRUE FALSE FALSE FALSE
phone_numbers[odd_index] <- 0
phone_numbers
## [1] 2 0 0 0 0 0 0 0 0 4
In the code chunk below:
fifty_rolls_1fifty_rolls_2# this function makes it so that your "random" number generator is at the same spot as mine.
set.seed(102389) ## leave this here to keep the randomness consistent
roll_six_sided_die <- function(){
die_rolls <- 1:6
die_probs <- c(rep(1/6,6))
rolls <- sample (die_rolls,1, replace = T, prob = die_probs)
return(rolls)
}
fifty_rolls_1 <- sapply(1:100,function(x) roll_six_sided_die())
fifty_rolls_1
## [1] 3 4 2 1 6 3 3 6 5 5 5 1 6 4 4 2 3 5 6 4 5 3 3 2 4 1 2 5 6 6 4 3 3 1 6 5 2
## [38] 6 1 1 4 2 2 6 1 6 3 5 2 6 3 6 1 5 3 5 4 3 6 1 1 6 3 3 3 4 1 1 2 4 1 1 2 1
## [75] 2 1 4 1 4 2 1 6 4 1 2 5 3 3 3 4 4 5 6 1 6 1 4 5 3 5
fifty_rolls_2 <- sapply(1:100,function(x) roll_six_sided_die())
fifty_rolls_2
## [1] 2 1 2 3 4 6 1 4 5 3 1 5 6 6 6 4 2 3 2 2 3 5 2 4 3 6 6 5 5 4 4 6 4 6 5 5 2
## [38] 3 6 6 1 4 4 6 3 6 4 2 6 5 3 3 4 4 2 1 5 6 2 6 5 5 1 5 1 2 1 6 6 2 6 1 6 1
## [75] 1 6 4 4 3 5 3 4 6 6 6 5 6 3 4 4 2 4 6 3 1 1 2 1 3 5
double <- sum(fifty_rolls_1 == fifty_rolls_2)
double
## [1] 21
Extra Credit, 5 points:
count_doubles <- function(){
set.seed(102389)
roll_six_sided_die <- function(){
die_rolls <- 1:6
die_probs <- c(rep(1/6,6))
rolls <- sample (die_rolls,1, replace = T, prob = die_probs)
return(rolls)
}
fifty_rolls_1 <- sapply(1:100,function(x) roll_six_sided_die())
print(fifty_rolls_1)
fifty_rolls_2 <- sapply(1:100,function(x) roll_six_sided_die())
print(fifty_rolls_2)
double <- sum(fifty_rolls_1 == fifty_rolls_2)
return(double)
}
count_doubles()
## [1] 3 4 2 1 6 3 3 6 5 5 5 1 6 4 4 2 3 5 6 4 5 3 3 2 4 1 2 5 6 6 4 3 3 1 6 5 2
## [38] 6 1 1 4 2 2 6 1 6 3 5 2 6 3 6 1 5 3 5 4 3 6 1 1 6 3 3 3 4 1 1 2 4 1 1 2 1
## [75] 2 1 4 1 4 2 1 6 4 1 2 5 3 3 3 4 4 5 6 1 6 1 4 5 3 5
## [1] 2 1 2 3 4 6 1 4 5 3 1 5 6 6 6 4 2 3 2 2 3 5 2 4 3 6 6 5 5 4 4 6 4 6 5 5 2
## [38] 3 6 6 1 4 4 6 3 6 4 2 6 5 3 3 4 4 2 1 5 6 2 6 5 5 1 5 1 2 1 6 6 2 6 1 6 1
## [75] 1 6 4 4 3 5 3 4 6 6 6 5 6 3 4 4 2 4 6 3 1 1 2 1 3 5
## [1] 21