0
0
R Programmingprogramming~30 mins

Handling missing values (drop_na, fill) in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Handling missing values (drop_na, fill)
📖 Scenario: You work as a data analyst. You have a small table of sales data, but some values are missing. You want to clean the data by removing rows with missing values and then fill missing values in another column.
🎯 Goal: Build an R script that creates a data frame with missing values, removes rows with missing values in one column, fills missing values in another column, and prints the cleaned data.
📋 What You'll Learn
Create a data frame called sales with columns product, price, and quantity including some missing values
Create a variable called cleaned_sales that removes rows with missing price using drop_na()
Fill missing quantity values in cleaned_sales using fill() from the tidyr package
Print the final cleaned_sales data frame
💡 Why This Matters
🌍 Real World
Data cleaning is a common task in data analysis to prepare data for accurate insights.
💼 Career
Knowing how to handle missing data is essential for data analysts, scientists, and anyone working with real-world data.
Progress0 / 4 steps
1
Create the sales data frame with missing values
Create a data frame called sales with these exact columns and values: product as c("A", "B", "C", "D"), price as c(10, NA, 15, 20), and quantity as c(5, 3, NA, 8).
R Programming
Need a hint?

Use data.frame() to create the table. Use NA for missing values.

2
Remove rows with missing price values
Load the tidyr package and create a new variable called cleaned_sales by removing rows with missing price from sales using drop_na(price).
R Programming
Need a hint?

Use library(tidyr) to load the package. Use drop_na() with the price column.

3
Fill missing quantity values
Use fill() from the tidyr package on cleaned_sales to fill missing quantity values downward. Update cleaned_sales with the result.
R Programming
Need a hint?

Use fill(cleaned_sales, quantity) to fill missing values downward.

4
Print the cleaned sales data frame
Write a line to print the cleaned_sales data frame to show the final cleaned data.
R Programming
Need a hint?

Use print(cleaned_sales) to display the data frame.