0
0
R Programmingprogramming~15 mins

filter() for row selection in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Filter Rows in a Data Frame Using filter()
📖 Scenario: You work in a small bookstore and have a list of books with their prices and stock. You want to find which books cost less than $20 to plan a sale.
🎯 Goal: Build a small R program that creates a data frame of books, sets a price limit, filters the books cheaper than that limit using filter(), and prints the filtered list.
📋 What You'll Learn
Create a data frame called books with columns title, price, and stock with exact values
Create a variable called max_price set to 20
Use dplyr::filter() with books and price < max_price to get cheaper books
Print the filtered data frame
💡 Why This Matters
🌍 Real World
Filtering data frames is a common task in data analysis to focus on relevant information, like finding affordable products or available stock.
💼 Career
Data analysts and scientists often use filtering to clean and prepare data before making reports or decisions.
Progress0 / 4 steps
1
Create the books data frame
Create a data frame called books with these exact entries:
title: "Book A", "Book B", "Book C", "Book D"
price: 15, 25, 10, 30
stock: 5, 2, 8, 1
R Programming
Need a hint?

Use data.frame() with vectors for each column named title, price, and stock.

2
Set the maximum price limit
Create a variable called max_price and set it to 20
R Programming
Need a hint?

Just assign the number 20 to a variable named max_price.

3
Filter books cheaper than max_price
Use dplyr::filter() with books and the condition price < max_price to create a new data frame called cheap_books
R Programming
Need a hint?

Remember to load dplyr with library(dplyr) before using filter().

4
Print the filtered books
Write a print() statement to display the cheap_books data frame
R Programming
Need a hint?

Use print(cheap_books) to show the filtered books.