Bird
Raised Fist0
R Programmingprogramming~20 mins

Pipe chaining operations in R Programming - Practice Problems & Coding Challenges

Choose your learning style10 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 chaining with dplyr filter and mutate

What is the output of this R code using pipe chaining?

R Programming
library(dplyr)
data <- tibble(x = 1:5, y = c(2, 4, 6, 8, 10))
result <- data %>% filter(x > 2) %>% mutate(z = y / x) %>% pull(z)
print(result)
A[1] 2 2 2
B[1] 3 2 2
C[1] 2 2 3
D[1] 2 3 2
Attempts:
2 left
💡 Hint

Remember filter keeps rows where x > 2, then z is y / x.

🧠 Conceptual
intermediate
1:30remaining
Understanding pipe chaining with base R and magrittr

Which option correctly describes what this pipe chain does?

R Programming
library(magrittr)
result <- 1:5 %>% sum() %>% sqrt()
ACalculates the sum of numbers 1 to 5 and then squares the result
BCalculates the square root of the sum of numbers 1 to 5
CCalculates the sum of the square roots of numbers 1 to 5
DCalculates the square root of each number from 1 to 5 and sums them
Attempts:
2 left
💡 Hint

Remember pipe passes the result of one function as input to the next.

🔧 Debug
advanced
2:00remaining
Identify the error in this pipe chain

What error does this R code produce?

R Programming
library(dplyr)
data <- tibble(a = 1:3, b = 4:6)
result <- data %>% select(a) %>% filter(b > 4)
AError: unexpected symbol in filter
BNo error, returns filtered tibble
CError: object 'b' not found
DError: select() expects at least one column
Attempts:
2 left
💡 Hint

Check which columns remain after select(a).

📝 Syntax
advanced
1:30remaining
Which pipe chaining syntax is correct?

Which option shows the correct pipe chaining syntax in R?

Adata %>% filter(x > 1) %>% mutate(y = x * 2)
Bdata |> filter(x > 1) %>% mutate(y = x * 2)
Cdata %>% filter(x > 1) |> mutate(y = x * 2)
Ddata |> filter(x > 1) |> mutate(y = x * 2)
Attempts:
2 left
💡 Hint

Check if the pipe operators are used consistently.

🚀 Application
expert
3:00remaining
Result of complex pipe chaining with multiple functions

What is the value of final after running this code?

R Programming
library(dplyr)
library(magrittr)
data <- tibble(val = c(2, 4, 6, 8))
final <- data %>%
  mutate(sq = val^2) %>%
  filter(sq %% 8 == 0) %>%
  summarise(total = sum(sq)) %>%
  pull(total)
print(final)
A100
B64
C120
D80
Attempts:
2 left
💡 Hint

Calculate squares, filter those divisible by 8, then sum.

Practice

(1/5)
1.

What does the pipe operator %>% do in R?

easy
A. It creates a new variable to store intermediate results.
B. It comments out the rest of the line in the code.
C. It passes the output of one function as input to the next function.
D. It repeats the previous function multiple times.

Solution

  1. Step 1: Understand the pipe operator purpose

    The pipe operator %>% takes the result from the left side and passes it as the first argument to the function on the right side.
  2. Step 2: Compare options with pipe behavior

    Only It passes the output of one function as input to the next function. correctly describes this behavior; other options describe unrelated actions.
  3. Final Answer:

    It passes the output of one function as input to the next function. -> Option C
  4. Quick Check:

    Pipe operator = passes output to next function [OK]
Hint: Pipe means pass output directly to next function [OK]
Common Mistakes:
  • Thinking pipe creates new variables
  • Confusing pipe with comment symbol
  • Assuming pipe repeats functions
2.

Which of the following is the correct syntax to use pipe chaining in R?

library(dplyr)
data <- data.frame(x = 1:3, y = 4:6)
data %>% ? %>% head()
easy
A. filter[x > 1]
B. filter(x > 1)
C. filter{ x > 1 }
D. filter x > 1

Solution

  1. Step 1: Identify correct function call syntax in pipe

    In R, functions are called with parentheses enclosing arguments, e.g., filter(x > 1).
  2. Step 2: Check options for correct syntax

    Only filter(x > 1) uses parentheses correctly; others use invalid syntax for function calls.
  3. Final Answer:

    filter(x > 1) -> Option B
  4. Quick Check:

    Function calls need parentheses [OK]
Hint: Use parentheses for function arguments in pipe [OK]
Common Mistakes:
  • Omitting parentheses in function calls
  • Using braces or brackets instead of parentheses
  • Writing function and arguments without separator
3.

What is the output of this R code?

library(dplyr)
data <- tibble(x = 1:5, y = 6:10)
data %>% filter(x > 2) %>% mutate(z = x + y) %>% pull(z)
medium
A. [1] 11 13 15
B. [1] 3 4 5
C. [1] 7 8 9
D. [1] 6 7 8

Solution

  1. Step 1: Filter rows where x > 2

    Original data: x = 1,2,3,4,5; y = 6,7,8,9,10. filter(x > 2) keeps x=3 (y=8), x=4 (y=9), x=5 (y=10).
  2. Step 2: Mutate z = x + y and pull(z)

    z values: 3+8=11, 4+9=13, 5+10=15. pull(z) outputs [1] 11 13 15.
  3. Final Answer:

    [1] 11 13 15 -> Option A
  4. Quick Check:

    filter(x > 2) %>% mutate(z = x + y) %>% pull(z) = [1] 11 13 15 [OK]
Hint: Follow pipe steps left to right carefully [OK]
Common Mistakes:
  • Not applying filter before mutate
  • Mixing up x and y values
  • Ignoring pull() extracts vector
4.

Find the error in this pipe chain and select the fix:

library(dplyr)
data <- tibble(a = 1:4, b = 5:8)
data %>% filter(a > 2) %>% mutate(c = a + b) %>% select(c, d)
medium
A. Remove mutate(c = a + b)
B. Add filter(b > 5) before mutate
C. Replace %>% with +
D. Change select(c, d) to select(c)

Solution

  1. Step 1: Identify error in select()

    The code tries to select columns c and d, but d does not exist in the data.
  2. Step 2: Fix select() to only include existing columns

    Changing select(c, d) to select(c) removes the error by selecting only the existing column c.
  3. Final Answer:

    Change select(c, d) to select(c) -> Option D
  4. Quick Check:

    Select only existing columns to avoid error [OK]
Hint: Select only columns present in data [OK]
Common Mistakes:
  • Selecting non-existent columns
  • Using wrong operators instead of pipe
  • Removing needed mutate step
5.

Using pipe chaining, how can you transform this data frame df to get the sum of squares of even numbers in column num?

df <- data.frame(num = 1:6)

Choose the correct pipe chain.

hard
A. df %>% filter(num %% 2 == 0) %>% mutate(sq = num^2) %>% summarise(total = sum(sq))
B. df %>% mutate(sq = num^2) %>% filter(num %% 2 == 0) %>% sum(sq)
C. df %>% filter(num %% 2 == 0) %>% summarise(total = sum(num))
D. df %>% summarise(total = sum(num^2)) %>% filter(num %% 2 == 0)

Solution

  1. Step 1: Filter even numbers from column num

    Use filter(num %% 2 == 0) to keep only even numbers.
  2. Step 2: Create a new column sq with squares of num

    Use mutate(sq = num^2) to add squared values.
  3. Step 3: Summarise total sum of sq column

    Use summarise(total = sum(sq)) to get the sum of squares.
  4. Final Answer:

    df %>% filter(num %% 2 == 0) %>% mutate(sq = num^2) %>% summarise(total = sum(sq)) -> Option A
  5. Quick Check:

    Filter -> mutate -> summarise = sum squares of evens [OK]
Hint: Filter first, then mutate, then summarise [OK]
Common Mistakes:
  • Summarising before filtering
  • Using sum() incorrectly inside pipe
  • Mutating after summarise
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard