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."
本练习是课程的一部分
String Manipulation with stringr in R
练习说明
- Print
my_toppingsto see your random toppings. - Add
"and "to the start of the third element by usingpaste()withmy_toppingsand a vector you define. - Create a vector
these_toppingsby usingpaste()to collapsemy_toppings_andwith a comma and space between each element. - Create
my_orderby pasting"I want to order a pizza with "tothese_toppingsand 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!
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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()
___