Let's order pizza!
As a final exercise in using paste()
and to celebrate getting to the end of the first chapter, let's order some pizza.
We've got a list of possible pizza toppings in toppings
.
You are going to randomly select three toppings, and then put them together using paste()
into an order for pizza, that should result in a string like,
"I want to order a pizza with mushrooms, spinach, and pineapple."
This exercise is part of the course
String Manipulation with stringr in R
Exercise instructions
- Print
my_toppings
to see your random toppings. - Add
"and "
to the start of the third element by usingpaste()
withmy_toppings
and a vector you define. - Create a vector
these_toppings
by usingpaste()
to collapsemy_toppings_and
with a comma and space between each element. - Create
my_order
by pasting"I want to order a pizza with "
tothese_toppings
and ending with a period,"."
. - Order your pizza by calling
writeLines()
onmy_order
. - Try re-running all your code (including the sampling of toppings). You should get a brand new pizza order!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Randomly sample 3 toppings
my_toppings <- sample(toppings, size = 3)
# Print my_toppings
___
# Paste "and " to last element: my_toppings_and
my_toppings_and <- ___
# Collapse with comma space: these_toppings
these_toppings <- ___
# Add rest of sentence: my_order
my_order <- ___
# Order pizza with writeLines()
___