Vector selection: the good times (3)
Selecting multiple elements of poker_vector
with c(2, 3, 4)
is not very convenient. Statisticians created an easier way to do this: c(2, 3, 4)
can be abbreviated to2:4
, which generates a vector with all natural numbers from 2 up to 4.
So, another way to find the mid-week results is poker_vector[2:4]
. Notice how the vector 2:4
is placed between the square brackets to select element 2 up to 4.
This exercise is part of the course
Introduction to R
Exercise instructions
Assign to roulette_selection_vector
the roulette results from Tuesday up to Friday; make use of :
if it makes things easier for you.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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
roulette_selection_vector <-