0
0
R Programmingprogramming~15 mins

Sorting with order() in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Sorting a List of Fruits by Price Using order() in R
📖 Scenario: You run a small fruit shop and keep a list of fruits with their prices. You want to sort the fruits by their prices to decide which ones to promote.
🎯 Goal: Learn how to use the order() function in R to sort a list of fruits by their prices in ascending order.
📋 What You'll Learn
Create a vector called fruits with the values "apple", "banana", "cherry", "date", "elderberry"
Create a numeric vector called prices with the values 3.5, 2.0, 4.0, 5.5, 1.5
Create a variable called sorted_indices using the order() function on prices
Use sorted_indices to create a new vector called sorted_fruits that sorts fruits by price
Print the sorted_fruits vector
💡 Why This Matters
🌍 Real World
Sorting items by price helps shop owners decide which products to promote or discount.
💼 Career
Understanding sorting techniques is essential for data analysis and organizing information efficiently in many jobs.
Progress0 / 4 steps
1
Create the fruits and prices vectors
Create a vector called fruits with the values "apple", "banana", "cherry", "date", "elderberry". Then create a numeric vector called prices with the values 3.5, 2.0, 4.0, 5.5, 1.5.
R Programming
Need a hint?

Use the c() function to create vectors in R.

2
Create sorted indices using order()
Create a variable called sorted_indices by using the order() function on the prices vector.
R Programming
Need a hint?

The order() function returns the positions that would sort the vector.

3
Sort fruits using sorted indices
Create a new vector called sorted_fruits by using sorted_indices to reorder the fruits vector.
R Programming
Need a hint?

Use square brackets [] to reorder a vector by indices.

4
Print the sorted fruits
Print the sorted_fruits vector to display the fruits sorted by price.
R Programming
Need a hint?

Use the print() function to show the vector.