Recall & Review
beginner
What is the purpose of the pipe operator (%>%) in R?
The pipe operator (%>%) passes the output of one function directly as the input to the next function, making code easier to read and write by chaining operations.
Click to reveal answer
beginner
How does pipe chaining improve code readability?
Pipe chaining lets you write a sequence of operations in a clear, step-by-step style, similar to giving instructions in order, avoiding nested functions and temporary variables.
Click to reveal answer
intermediate
In the pipe chain: mtcars %>% filter(cyl == 6) %>% summarise(avg_mpg = mean(mpg)), what does each step do?
First, mtcars data is passed to filter() to keep only rows where cyl equals 6. Then, the filtered data is passed to summarise() to calculate the average mpg for those rows.
Click to reveal answer
beginner
What happens if you forget to load the dplyr package before using %>%, and how to fix it?
You will get an error because %>% is defined in dplyr or magrittr packages. Fix it by running library(dplyr) or library(magrittr) before using the pipe.
Click to reveal answer
intermediate
Can you use pipe chaining with base R functions? Give an example.
Yes, you can use pipe chaining with base R functions if they accept the first argument as the data input. Example: mtcars %>% subset(cyl == 4) %>% summary()
Click to reveal answer
What does the pipe operator (%>%) do in R?
✗ Incorrect
The pipe operator (%>%) passes the result of the left side as the first argument to the function on the right side.
Which package must you load to use %>% in R?
✗ Incorrect
The %>% operator is provided by the dplyr or magrittr package, so you need to load dplyr to use it.
In pipe chaining, what is the main benefit compared to nested functions?
✗ Incorrect
Pipe chaining improves readability by writing operations in a clear, linear sequence.
What will this code do? mtcars %>% filter(cyl == 4) %>% summarise(max_hp = max(hp))
✗ Incorrect
It filters mtcars to only 4-cylinder cars, then calculates the maximum horsepower among them.
Can you use pipe chaining with base R functions like subset()?
✗ Incorrect
Base R functions that take data as the first argument can be used with pipes.
Explain how pipe chaining (%>%) works in R and why it is useful.
Think about how you can write step-by-step instructions for data.
You got /4 concepts.
Describe a simple example using pipe chaining to filter a dataset and then summarize it.
Start with a dataset, then filter rows, then calculate a summary.
You got /4 concepts.