Calculating total winnings (3)

Based on the previous analysis, it looks like you had a mix of good and bad days. This is not what your ego expected, and you wonder if there may be a very tiny chance you have lost money over the week in total?

A function that helps you to answer this question is sum(). It calculates the sum of all elements of a vector. For example, to calculate the total amount of money you have lost/won with poker you do:

total_poker <- sum(poker_vector)

Instruction

# Poker and roulette winnings from Monday to Friday: poker_vector <- c(140, -50, 20, -120, 240) roulette_vector <- c(-24, -50, 100, -350, 10) days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") names(poker_vector) <- days_vector names(roulette_vector) <- days_vector # Total winnings with poker total_poker <- sum(poker_vector) # Total winnings with roulette total_roulette <- # Total winnings overall total_week <- # Print out total_week # Poker and roulette winnings from Monday to Friday: poker_vector <- c(140, -50, 20, -120, 240) roulette_vector <- c(-24, -50, 100, -350, 10) days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday") names(poker_vector) <- days_vector names(roulette_vector) <- days_vector # Total winnings with poker total_poker <- sum(poker_vector) # Total winnings with roulette total_roulette <- sum(roulette_vector) # Total winnings overall total_week <- total_roulette + total_poker # Print out total_week total_week msg = "Do not change anything about the definition and naming of `poker_vector` and `roulette_vector`." test_object("days_vector", undefined_msg = msg, incorrect_msg = msg) test_object("poker_vector", eq_condition = "equal", undefined_msg = msg, incorrect_msg = msg) test_object("roulette_vector", eq_condition = "equal", undefined_msg = msg, incorrect_msg = msg) test_object("total_poker", incorrect_msg = "Make sure that you assign to `total_poker` the sum of the `poker_vector`.") test_object("total_roulette", incorrect_msg = "Make sure that you assign to `total_roulette` the sum of the `roulette_vector`.") test_object("total_week", incorrect_msg = "Make sure that you assign to `total_week` the sum of the other two total vectors: `total_roulette` and `total_poker`.") test_output_contains("total_week", incorrect_msg = "Don't forget to write `total_week` on a new line to print out the variable.") success_msg("Well done. This is pretty bad news...")

Use the sum() function to get the total of the roulette_vector. total_week is then the sum of roulette_vector and poker_vector.

Previous: 2-7 | Calculating total winnings (2)

Next: 2-9 | Comparing total winnings

Back to Main