📖 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 = 3
Create a variable called price_threshold and set it to 1
Use the %>% pipe operator to filter fruit_prices for prices greater than price_threshold and then sum the filtered prices
Use the |> pipe operator to do the same filtering and summing in a separate variable
Print 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
1
Create the fruit prices vector
Create a named vector called fruit_prices with these exact entries: apple = 2, banana = 1, cherry = 3
R Programming
Hint
Use the c() function with named elements to create fruit_prices.
2
Set the price threshold
Create a variable called price_threshold and set it to 1
R Programming
Hint
Just assign the number 1 to price_threshold.
3
Calculate total price using %>% pipe
Use the %>% pipe operator to filter fruit_prices for prices greater than price_threshold and then sum the filtered prices. Store the result in total_price_percent. Use the exact code total_price_percent <- fruit_prices %>% .[. > price_threshold] %>% sum()
R Programming
Hint
Use %>% to pass fruit_prices through filtering and then summing.
4
Calculate total price using |> pipe and print results
Use the |> pipe operator to filter fruit_prices for prices greater than price_threshold and then sum the filtered prices. Store the result in total_price_base. Then print both total_price_percent and total_price_base using two separate print() statements.
R Programming
Hint
Use |> with an anonymous function to filter, then pipe to sum(). Use print() to show results.
Practice
(1/5)
1. What does the pipe operator %>% do in R?
easy
A. It creates a new variable in the environment.
B. It passes the result of one function as the first argument to the next function.
C. It comments out the following line of code.
D. It repeats a function multiple times automatically.
Solution
Step 1: Understand the pipe operator purpose
The pipe operator %>% takes the output of the left side and sends it as the first argument to the function on the right side.
Step 2: Compare with other options
Options B, C, and D describe unrelated actions like creating variables, commenting, or repeating functions, which are not what pipes do.
Final Answer:
It passes the result of one function as the first argument to the next function. -> Option B
Quick Check:
Pipe operator = passes output to next function [OK]
Hint: Pipes send output forward as input to next function [OK]
Common Mistakes:
Thinking pipe creates variables
Confusing pipe with comment symbol
Assuming pipe repeats functions
2. Which of the following is the correct syntax to use the native R pipe operator introduced in R 4.1?
easy
A. data |> filter(value > 10)
B. data %>% filter(value > 10)
C. data ->| filter(value > 10)
D. data |-> filter(value > 10)
Solution
Step 1: Identify native pipe syntax
The native pipe operator in R 4.1+ is |>, used as data |> function().
Step 2: Check other options
data %>% filter(value > 10) uses %>% which is from dplyr, not native. Options A and D are invalid syntax.
Final Answer:
data |> filter(value > 10) -> Option A
Quick Check:
Native pipe = |> [OK]
Hint: Native pipe uses |> without percent signs [OK]
Common Mistakes:
Using %>% as native pipe
Typing invalid pipe symbols like ->|
Confusing pipe with assignment operators
3. What is the output of this R code?
library(dplyr)
data <- tibble(x = 1:3)
data %>% mutate(y = x * 2) %>% pull(y)
medium
A. [1, 2, 3]
B. Error: object 'y' not found
C. [1, 3, 5]
D. [2, 4, 6]
Solution
Step 1: Understand the pipe steps
The data tibble has x = 1, 2, 3. The mutate(y = x * 2) adds y as double x: 2, 4, 6.
Step 2: Extract the y column
pull(y) extracts the y column as a vector: 2, 4, 6.
Final Answer:
[2, 4, 6] -> Option D
Quick Check:
Mutate doubles x, pull extracts y [OK]
Hint: Mutate adds column, pull extracts it [OK]
Common Mistakes:
Forgetting mutate creates y
Thinking pull returns whole data frame
Confusing x and y values
4. Identify the error in this R code using the native pipe operator:
data <- data.frame(a = 1:3)
data |> filter(a > 1)
medium
A. filter() is not found because dplyr is not loaded
B. Native pipe operator |> is invalid syntax here
C. data.frame cannot be piped
D. No error, code runs fine
Solution
Step 1: Check package usage
The function filter() is from dplyr. If dplyr is not loaded, filter() is not available.
Step 2: Validate pipe and data
The native pipe |> works fine with data frames. The error is due to missing dplyr, not pipe or data type.
Final Answer:
filter() is not found because dplyr is not loaded -> Option A
Quick Check:
Missing dplyr causes filter error [OK]
Hint: Load dplyr before using filter() [OK]
Common Mistakes:
Assuming pipe syntax is wrong
Thinking data.frame can't be piped
Ignoring missing library calls
5. Using the native pipe operator, how would you transform a numeric vector v <- c(1, 2, 3, 4) to get the sum of squares of even numbers only?
hard
A. v %>% subset(v %% 2 == 0) %>% sum()^2
B. v |> filter(v %% 2 == 0) |> sum()^2
C. v |> \(x) x[x %% 2 == 0] |> \(x) sum(x^2)
D. v |> map(~ .x^2) |> sum()
Solution
Step 1: Filter even numbers using native pipe and anonymous function
The expression \(x) x[x %% 2 == 0] selects even numbers from vector v.
Step 2: Sum squares of filtered numbers
The next pipe applies \(x) sum(x^2) to calculate sum of squares of those even numbers.
Final Answer:
v |> \(x) x[x %% 2 == 0] |> \(x) sum(x^2) -> Option C
Quick Check:
Filter evens then sum squares [OK]
Hint: Use anonymous functions with |> for complex steps [OK]
Common Mistakes:
Using filter() without loading dplyr
Squaring sum instead of summing squares
Using %>% with native pipe syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.