📖 Scenario: Imagine you are a small business owner who wants to keep track of your sales data. You have information about the products sold, the quantity, and the price. You want to organize this data so you can easily analyze it and make decisions.
🎯 Goal: You will create a simple data frame in R to store sales data, add a helper variable for a sales threshold, filter the data based on this threshold, and then display the filtered results. This will show why data frames are central to R for handling tabular data.
📋 What You'll Learn
Create a data frame called sales_data with columns Product, Quantity, and Price using exact values
Create a variable called min_sales with the value 50
Use a subset operation to create a new data frame filtered_sales with rows where Quantity is greater than min_sales
Print the filtered_sales data frame
💡 Why This Matters
🌍 Real World
Data frames help organize and analyze data like sales, customer info, or survey results in a clear table format.
💼 Career
Many jobs in data analysis, statistics, and research use R data frames to handle and explore data efficiently.
Progress0 / 4 steps
1
Create the sales data frame
Create a data frame called sales_data with these exact columns and values: Product with values 'Apples', 'Bananas', 'Cherries'; Quantity with values 30, 60, 45; and Price with values 1.2, 0.5, 2.0.
R Programming
Hint
Use the data.frame() function with named vectors for each column.
2
Set the minimum sales threshold
Create a variable called min_sales and set it to the number 50.
R Programming
Hint
Just assign the number 50 to the variable min_sales.
3
Filter the sales data by quantity
Create a new data frame called filtered_sales that contains only the rows from sales_data where the Quantity is greater than min_sales.
R Programming
Hint
Use the subset() function to filter rows where Quantity is greater than min_sales.
4
Display the filtered sales data
Print the filtered_sales data frame to show the products with quantity greater than min_sales.
R Programming
Hint
Use the print() function to display the filtered_sales data frame.
Practice
(1/5)
1. Why are data frames considered central to R programming?
easy
A. Because they only store numeric data efficiently
B. Because they organize data in rows and columns, allowing easy data analysis
C. Because they are used to create graphics directly
D. Because they replace all other data structures in R
Solution
Step 1: Understand data frame structure
Data frames store data in rows and columns, similar to a spreadsheet, making data easy to view and analyze.
Step 2: Recognize their role in R
They allow different types of data in each column and are widely used for data manipulation and analysis.
Final Answer:
Because they organize data in rows and columns, allowing easy data analysis -> Option B
Quick Check:
Data frames = central for organizing data [OK]
Hint: Think spreadsheet style data storage [OK]
Common Mistakes:
Assuming data frames only store numeric data
Confusing data frames with plotting functions
Thinking data frames replace all data types
2. Which of the following is the correct way to create a simple data frame in R?
easy
A. df <- dataframe(name = ['Anna', 'Bob'], age = [25, 30])
B. df <- data.frame('Anna', 25, 'Bob', 30)
C. df <- data.frame(name = c('Anna', 'Bob'), age = c(25, 30))
D. df <- data.frame(name = 'Anna', age = 25, name = 'Bob', age = 30)
Solution
Step 1: Check syntax for data.frame()
The correct function is data.frame() with named arguments and vectors for each column.