0
0
R Programmingprogramming~20 mins

Pipe operator (%>% and |>) in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipe Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pipe with dplyr and base R
What is the output of this R code using the pipe operator %>% from dplyr?
R Programming
library(dplyr)
result <- mtcars %>%
  filter(cyl == 6) %>%
  summarise(avg_mpg = mean(mpg)) %>%
  pull(avg_mpg)
print(round(result, 2))
A19.74
B20.57
C21.00
D18.50
Attempts:
2 left
💡 Hint
Look at the cars with 6 cylinders and calculate their average mpg.
Predict Output
intermediate
2:00remaining
Output of base R pipe |> with nested functions
What is the output of this R code using the base pipe operator |>?
R Programming
result <- 1:5 |> sum() |> sqrt()
print(round(result, 3))
A2.236
B5.477
C15.000
D3.873
Attempts:
2 left
💡 Hint
Sum 1 to 5, then take the square root.
🔧 Debug
advanced
2:00remaining
Identify the error in pipe usage
What error does this R code produce when using the pipe operator %>%?
R Programming
library(dplyr)
result <- mtcars %>%
  filter(cyl == 4) %>%
  summarise(avg_hp = mean(hp)) %>%
  select(avg_hp) %>%
  mean()
print(result)
AError: no applicable method for 'mean' applied to an object of class 'data.frame'
BError: argument is not numeric or logical: returning NA
CError: object of type 'closure' is not subsettable
DNo error, prints average horsepower
Attempts:
2 left
💡 Hint
Check what class the object is before calling mean().
🧠 Conceptual
advanced
2:00remaining
Understanding pipe operator behavior
Which statement best describes the difference between %>% and |> in R?
A%>% is deprecated and replaced by |> in all R versions.
B%>% is from magrittr and allows placeholder .; |> is base R pipe introduced in R 4.1 without placeholder support.
C%>% is base R pipe; |> is from magrittr package with more features.
DBoth %>% and |> are identical in syntax and behavior.
Attempts:
2 left
💡 Hint
Think about package origin and placeholder usage.
Predict Output
expert
2:00remaining
Output of complex pipe with anonymous function
What is the output of this R code using the base pipe |> with an anonymous function?
R Programming
result <- 1:4 |> ((x) x * 2)() |> sum()
print(result)
AError: unexpected '()' in expression
B10
C20
D30
Attempts:
2 left
💡 Hint
Anonymous function doubles each element, then sum them.