Using Pipe Operators %>% and |> in R
📖 Scenario: You are working with a small dataset of fruits and their prices. You want to cleanly and clearly calculate the total cost of all fruits using pipe operators to make your code easy to read.
🎯 Goal: Build a simple R script that uses the pipe operators %>% and |> to calculate the total price of fruits from a named vector.
📋 What You'll Learn
Create a named vector called
fruit_prices with exact entries: apple = 2, banana = 1, cherry = 3Create a variable called
price_threshold and set it to 1Use the
%>% pipe operator to filter fruit_prices for prices greater than price_threshold and then sum the filtered pricesUse the
|> pipe operator to do the same filtering and summing in a separate variablePrint both results with exact variable names
total_price_percent and total_price_base💡 Why This Matters
🌍 Real World
Pipes help write clear and readable data processing steps, like filtering and summarizing prices in a shopping list.
💼 Career
Data analysts and scientists use pipe operators to write clean, easy-to-understand code for data cleaning and analysis.
Progress0 / 4 steps