“We will be honest in all our academic activities and will not tolerate dishonesty.”
– UHD Academic Honor Code
You are submitting this exam under the UHD honor code. Answer all below questions. Complete the questions, knit the file to html, commit and push to your repository BEFORE THE END OF CLASS and turn in link to the repository to Canvas
There are 98 possible points, and 8 possible extra credit points.
Any commit made after the class period is over will not be graded.
Choose one of the two to answer:
numbers that contains
all the multiples of 3 from 3 to 99. (3, 6, 9, … 93, 96, 99). Do not
just type the values in.numbers <- c(seq(3,99,by=3))
print(numbers)
## [1] 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75
## [26] 78 81 84 87 90 93 96 99
numbers_plus_one that contains the elements of
numbers increased by 1.numbers_plus_one <- c(numbers +1)
print(numbers_plus_one)
## [1] 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46 49 52 55 58
## [20] 61 64 67 70 73 76 79 82 85 88 91 94 97 100
numbers by
numbers_plus_one and multiply the result by 100.numbers <- c((numbers/numbers_plus_one)*100)
print(numbers)
## [1] 75.00000 85.71429 90.00000 92.30769 93.75000 94.73684 95.45455 96.00000
## [9] 96.42857 96.77419 97.05882 97.29730 97.50000 97.67442 97.82609 97.95918
## [17] 98.07692 98.18182 98.27586 98.36066 98.43750 98.50746 98.57143 98.63014
## [25] 98.68421 98.73418 98.78049 98.82353 98.86364 98.90110 98.93617 98.96907
## [33] 99.00000
names <-c("Nia","Tinh","Taehee")
print(names)
## [1] "Nia" "Tinh" "Taehee"
names <- c(paste(names,"is awesome!", sep = " "))
print(names)
## [1] "Nia is awesome!" "Tinh is awesome!" "Taehee is awesome!"
You are running a food truck that has the following 5 meal options:
meal <- c("Chicken", "Tofu", "Steak", "Eggplant", "Fish")
price <- c(10, 8, 16, 10, 14)
calories <- c(450, 500, 600, 400, 550)
Find the below values using indexing or R functions (don’t just type “The cheapest meal is 8 dollars” - pretend that your data has 100,000,000 values and you can’t just look at it!)
avg_price <- mean(price)
avg_calories <- mean(calories)
print(paste(avg_price,avg_calories, sep = " "))
## [1] "11.6 500"
index <- which(meal == "Tofu")
tofu_calo <- calories[index]
print(tofu_calo)
## [1] 500
# we can combine like that : tofu_calos <- calories[which(meal == "Tofu")]
price to reflect that.price <-c(price +2)
print(price)
## [1] 12 10 18 12 16
meal <-c(meal, "fries")
calories <-c(calories,500)
price <-c(price,4)
data <- data.frame(Meals = meal, Calo = calories, Price = price)
print(data)
## Meals Calo Price
## 1 Chicken 450 12
## 2 Tofu 500 10
## 3 Steak 600 18
## 4 Eggplant 400 12
## 5 Fish 550 16
## 6 fries 500 4
In soccer, a team gets 3 points for a win and 1 point for a tie.
w and the number of ties t, and returns
the number of points that team has. Note: The number of points should be
returned as a numeric value, not printed or as part of a string.\[ points = 3 \times wins + 1 \times ties \]
point_calculate<-function(w,t){
result =0
result = 3*w + t
return(result)
}
#print(point_calculate(2,3))
point_calculate(10,6)
## [1] 36
x, and returns the third element of that vector. (Hard
coding here for the 3 is okay)third_element <- function(input_vector){
return(input_vector[3])
}
For example, if you pass the function the vector 1:10, it should return the number 3, which is the value in the third element of the vector.
1:10 and c(-4, 0, 4)third_element(1:10)
## [1] 3
third_element(c(-4,0,4))
## [1] 4
n, to your above function that specifies which
element to return.nth_element<-function(input_vector, n){
return(input_vector[n])
}
For example, if you pass this function the same vector 1:10 and the number 4, it should return the number 4, since the second argument is 4.
# get the 7th element of 1:10 and the 2nd element of c(-4,0,4)
nth_element(1:10,7)
## [1] 7
nth_element(c(-4,0,4),2)
## [1] 0
nth_element_improve<-function(input_vector, n){
if(n> length(input_vector))
{
print(paste("Input vector length is less than ",n,". Your element request is outbound (out of index) "))
}
else{
return(input_vector[n])
}
}
nth_element_improve(1:10,11)
## [1] "Input vector length is less than 11 . Your element request is outbound (out of index) "
A new club in town wants to program a robotic bouncer, and the first step is finding out if someone is old enough to come in.
age of
someone as an argument, and returns the following strings of responses
for the given age:age < 20 & age > 14 -
or just make it the “else”!check<-function(age){
response <-""
if(age <=13){
response <- "What are you doing here?!?"
}
else if (age < 21){
response <-"No drinks for you"
}
else
{
response <-"Good to go!"
}
return(response)
}
check(14)
## [1] "No drinks for you"
check(4)
## [1] "What are you doing here?!?"
check(18)
## [1] "No drinks for you"
check(22)
## [1] "Good to go!"
age_limit that can be used to adjust the
categories instead of just using 21.check2<-function(age,age_limit){
response <-""
if(age <=13){
response <- "What are you doing here?!?"
}
else if (age < age_limit){
response <-"No drinks for you"
}
else
{
response <-"Good to go!"
}
return(response)
}
check2(25,27)
## [1] "No drinks for you"
check2(20,19)
## [1] "Good to go!"