Vector selection: the good times

Your hunch seemed to be right. It appears that the poker game is more your cup of tea than roulette.

Another possible route for investigation is your performance at the beginning of the working week compared to the end of it. You did have a couple of Margarita cocktails at the end of the week...

To answer that question, you only want to focus on a selection of the total_vector. In other words, our goal is to select specific elements of the vector. To select elements of a vector (and later matrices, data frames, ...), you can use square brackets. Between the square brackets, you indicate what elements to select. For example, to select the first element of the vector, you type poker_vector[1]. To select the second element of the vector, you type poker_vector[2], etc. Notice that the first element in a vector has index 1, not 0 as in many other programming languages.

Instruction

Assign the poker results of Wednesday to the variable poker_wednesday.

# 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 # Define a new variable based on a selection poker_wednesday <- # 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 # Define a new variable based on a selection poker_wednesday <- poker_vector[3] 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("poker_wednesday", undefined_msg = "Please make sure to define a variable `poker_wednesday`.", incorrect_msg = "It looks like `poker_wednesday` does not contain the correct value of the `poker_vector`.") success_msg("Great! R also makes it possible to select multiple elements from a vector at once. Learn how in the next exercise!")

Wednesday is the third element of poker_vector, and can thus be selected with poker_vector[3].

Previous: 2-9 | Comparing total winnings

Next: 2-11 | Vector selection: the good times (2)

Back to Main