Question 1 1 pts Which one of the following R codes would run without any errors? value <-5 if(value > 5){ result <- 'more than 5 }else{ result <- 'less than or equal to 5' } value <- 5 if (> 5){ result <- 'more than 5' }else{ result <- 'less than or equal to 5' } O value <- 5 if(value > 5) result <- 'more than 5 }{ result <- 'less than or equal to 5' } value <- 5 if(value > 5){ result <- 'more than 5' else result <- 'less than or equal to 5' }
Suppose you are writing a function to compute the final price to collect from the clients given two inputs: • hours: a numerical vector that contains the number of hours worked for each client, • client_type: a text vector that contains the type of each client. Here, we compute net price as "hours" multiplied by hourly rate, which is 100. Then, the final price is calculated from the net price in the following way for each client: • if hours worked, given in hours, is larger than 100 hours, then a 10% discount to net,price is applied; • if the client is "public", given in client_type, then a 12% tax surcharge would be added to net price; • if the client is "private", given in client_type, then a 6% tax surcharge would be added to net price. The following questions are about how the incomplete code given below should be completed: charge_func <- function(hours, client_type){ final_price <-CO for(i in 1: ???) { # Calculate net price net_price <- hours*100 # Discount if(???) net_price <- net_price * 0.9 } # Add the tax when needed if(???){ final_price <- net_price* 1.12 # 12% VAT } else if (???) final_price <- net_price* 1.06 #6% VAT } else { final_price <- net_price* 1 #0% VAT } return(???)
Question 2 1 pts What would you put in the first ??? space: for(i in 1: ???) length(final_price) O length(hours) O nrow(hours) Onrow(final_price) Question 3 1 pts What would you put in the second ??? space: # Discount if(???){ net_price <- net_price * 0.9 } O hours > 100 O hours > 100 O net_price > 100 O net_price > 100
Question 4 1 pts What would you put in the two spaces marked as "???" to compute tax, for each client type: # Add the tax when needed if(???) total_price <- net_price * 1.12 # 12% VAT } else if (???){ total_price <- net_price* 1.06 #6% VAT } else { total_price <- net_price * 1 # 0% VAT. } The statement before ;' is for the first ??? and the statement after ;' is for the second ???. O client_type = 'public'; client_type = 'private client_type == 'public'; client_type == 'private client_type == 'public'; client_type[i] == 'private client_type[i] = 'public'; client_type[i] = 'private Question 5 1 pts What would you put as what the function returns? return(???) final_price[i] net_price[i] O net_price O final_price
Question 1 1 pts Which one of the following R codes would run without any errors? value <-5 if(value > 5){ result <- 'mo
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am