0
0
R Programmingprogramming~30 mins

mutate() for new columns in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Using mutate() to Add New Columns in R
📖 Scenario: You work in a small bakery and keep track of daily sales data in a table. You want to add new columns to calculate the total revenue and a label for high or low sales days.
🎯 Goal: Learn how to use mutate() from the dplyr package in R to add new columns to a data frame.
📋 What You'll Learn
Create a data frame called sales_data with exact columns and values
Create a variable called high_sales_threshold with a specific numeric value
Use mutate() to add two new columns: total_revenue and sales_label
Print the updated sales_data data frame
💡 Why This Matters
🌍 Real World
Adding new columns to data tables is common in business to calculate totals, labels, or categories based on existing data.
💼 Career
Data analysts and scientists often use mutate() in R to prepare and enrich data for reports and decision-making.
Progress0 / 4 steps
1
Create the initial sales data frame
Create a data frame called sales_data with columns day, items_sold, and price_per_item. Use these exact values: day as c("Mon", "Tue", "Wed", "Thu", "Fri"), items_sold as c(10, 15, 7, 20, 13), and price_per_item as c(2.5, 2.5, 2.5, 2.5, 2.5).
R Programming
Need a hint?

Use data.frame() to create the table with the exact column names and values.

2
Set the high sales threshold
Create a numeric variable called high_sales_threshold and set it to 30.
R Programming
Need a hint?

Just assign the number 30 to the variable high_sales_threshold.

3
Add new columns with mutate()
Use mutate() from the dplyr package on sales_data to add two new columns: total_revenue which is items_sold * price_per_item, and sales_label which is "High" if total_revenue is greater than high_sales_threshold, otherwise "Low". Assign the result back to sales_data. Remember to load dplyr with library(dplyr).
R Programming
Need a hint?

Use mutate() to add columns. Use ifelse() to create the sales_label based on the threshold.

4
Print the updated sales_data
Write a print() statement to display the updated sales_data data frame.
R Programming
Need a hint?

Use print(sales_data) to show the updated table.