Calculating total winnings

Now that you have the poker and roulette winnings nicely as named vectors, you can start doing some data analytical magic.

You want to find out the following type of information:

To get the answers, you have to do arithmetic calculations on vectors.

It is important to know that if you sum two vectors in R, it takes the element-wise sum. For example, the following three statements are completely equivalent:

c(1, 2, 3) + c(4, 5, 6)
c(1 + 4, 2 + 5, 3 + 6)
c(5, 7, 9)

You can also do the calculations with variables that represent vectors:

a <- c(1, 2, 3) 
b <- c(4, 5, 6)
c <- a + b

Instruction

A_vector <- c(1, 2, 3) B_vector <- c(4, 5, 6) # Take the sum of A_vector and B_vector total_vector <- # Print out total_vector A_vector <- c(1, 2, 3) B_vector <- c(4, 5, 6) # Take the sum of A_vector and B_vector total_vector <- A_vector + B_vector # Print out total_vector total_vector msg <- "Do not change the contents of `A_vector` or `B_vector`!" test_object("A_vector", undefined_msg = msg, incorrect_msg = msg) test_object("B_vector", undefined_msg = msg, incorrect_msg = msg) test_object("total_vector", incorrect_msg = "Make sure that `total_vector` contains the sum of `A_vector` and `B_vector`.") test_output_contains("total_vector", incorrect_msg = "Don't forget to print out `total_vector`! Simply write `total_vector` on a new line.") success_msg("Good job! Continue to the next exercise.")

Use the + operator to sum A_vector and B_vector. Use <- to assign the result to total_vector.

Previous: 2-5 | Naming a vector (2)

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

Back to Main