As a data analyst, it is important to have a clear view on the data that you are using. Understanding what each element refers to is therefore essential.
In the previous exercise, we created a vector with your winnings over the week. Each vector element refers to a day of the week but it is hard to tell which element belongs to which day. It would be nice if you could show that in the vector itself.
You can give a name to the elements of a vector with the names()
function. Have a look at this example:
some_vector <- c("John Doe", "poker player")
names(some_vector) <- c("Name", "Profession")
This code first creates a vector some_vector
and then gives the two elements a name. The first element is assigned the name Name
, while the second element is labeled Profession
. Printing the contents to the console yields following output:
Name Profession
"John Doe" "poker player"
poker_vector
with the days of the week. Add code to do the same thing for roulette_vector
.
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)
# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)
# Assign days as names of poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
# Assign days as names of roulette_vector
# Poker winnings from Monday to Friday
poker_vector <- c(140, -50, 20, -120, 240)
# Roulette winnings from Monday to Friday
roulette_vector <- c(-24, -50, 100, -350, 10)
# Assign days as names of poker_vector
names(poker_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
# Assign days as names of roulette_vector
names(roulette_vector) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday")
test_object("poker_vector",
incorrect_msg = "Do not change the values inside `poker_vector`; they were already coded for you.")
test_object("roulette_vector",
incorrect_msg = "Do not change the values inside `roulette_vector`; they were already coded for you.")
test_object("poker_vector",
eq_condition = "equal",
incorrect_msg = "Do not change the code that names the elements in `poker_vector`; focus on `roulette_vector`!")
test_object("roulette_vector",
eq_condition = "equal",
incorrect_msg = "Make sure that you assign the correct names vector to `roulette_vector`. Use the exact same vector as the one that was used to name `poker_vector`.")
success_msg("Well done! Continue to the next exercise.")
You can use names(roulette_vector)
to set the names of the variable roulette_vector
. Make sure to use the same vector with the days of the week as names. Remember that R is case sensitive!