“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.

Part 1 (20 points)

Part 2 (15 points)

Choose one of the two to answer:

  1. In your own words, describe why you might want to make your own function.
  1. In your own words, describe why comments and variable names are important parts of good coding style.

Part 3 (18 points)

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 <- 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 <- 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!"

Part 4 (10 points)

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 <-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

Part 5 (10 points)

In soccer, a team gets 3 points for a win and 1 point for a tie.

  1. [7 points] Write a function that takes two arguments, the number of wins 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))
  1. [3 points] Last season, the Houston Dash had 10 wins and 6 ties. Use your function to find out how many points they earned.
point_calculate(10,6)
## [1] 36

Part 6 (15+5 points)

  1. [6 points] Write a function that takes one argument, a vector 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. [3 points] Test your function twice by passing the following vectors as the argument: 1:10 and c(-4, 0, 4)
third_element(1:10)
## [1] 3
third_element(c(-4,0,4))
## [1] 4
  1. [6 points] Add an additional (second) argument, a number 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.

  1. [3 points] Test your second function on the vectors from above
# 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
  1. [5 extra credit points] You can’t take the 8th element of a vector that’s only 3 elements long! Add a step in the function code to check and make sure that the value of the second argument is less than or equal to the length of the vector
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) "

Part 7 (10+3 points)

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.

  1. [8 points] Write a function that takes the age of someone as an argument, and returns the following strings of responses for the given age:
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"
  1. [2 points] Test your function with the following ages: 4, 18, 22.
check(4)
## [1] "What are you doing here?!?"
check(18)
## [1] "No drinks for you"
check(22)
## [1] "Good to go!"
  1. [3 extra credit points] Modify your function so that it can be used in different countries that have different drinking ages: add a second argument 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!"