0
0
R Programmingprogramming~15 mins

Modifying and adding elements in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Modifying and adding elements
📖 Scenario: You are managing a list of fruits in a basket. Sometimes you need to change a fruit or add a new one to keep the basket fresh and interesting.
🎯 Goal: You will create a vector of fruits, then modify one fruit's name, add a new fruit, and finally display the updated basket.
📋 What You'll Learn
Create a vector called basket with the fruits: "apple", "banana", "cherry"
Create a variable called replace_index and set it to 2
Replace the fruit at position replace_index in basket with "blueberry"
Add the fruit "date" to the end of basket
Print the updated basket vector
💡 Why This Matters
🌍 Real World
Managing lists of items like groceries, tasks, or favorites often requires changing or adding elements to keep the list current.
💼 Career
Knowing how to modify and add elements in data structures is essential for data cleaning, updating records, and managing collections in many programming jobs.
Progress0 / 4 steps
1
Create the initial fruit basket
Create a vector called basket with the fruits "apple", "banana", and "cherry".
R Programming
Need a hint?

Use the c() function to create a vector with the exact fruits.

2
Set the index to replace
Create a variable called replace_index and set it to 2.
R Programming
Need a hint?

Just assign the number 2 to the variable replace_index.

3
Modify the fruit at the given index
Replace the fruit at position replace_index in basket with "blueberry".
R Programming
Need a hint?

Use square brackets [] to access and change the element at replace_index.

4
Add a new fruit and print the basket
Add the fruit "date" to the end of basket and then print the updated basket vector.
R Programming
Need a hint?

Use c() to add "date" to the vector and print() to show the basket.