0
0
R Programmingprogramming~30 mins

Debugging tools in R Programming - Mini Project: Build & Apply

Choose your learning style9 modes available
Debugging Tools in R
📖 Scenario: You are working on a small R script that calculates the average of a list of numbers. Sometimes, the script gives unexpected results or errors. To fix this, you want to learn how to use basic debugging tools in R.
🎯 Goal: Learn how to use simple debugging tools in R like print() statements and the browser() function to find and fix errors in your code.
📋 What You'll Learn
Create a numeric vector called numbers with specific values
Create a variable called threshold to use in a condition
Write a for loop to calculate the sum of numbers greater than the threshold
Use print() and browser() to debug the loop
Print the final sum
💡 Why This Matters
🌍 Real World
Debugging is a key skill when writing any program. Using print statements and breakpoints helps find where things go wrong.
💼 Career
Many programming jobs require you to debug code efficiently. Knowing how to pause and inspect your program helps fix bugs faster.
Progress0 / 4 steps
1
Create the data vector
Create a numeric vector called numbers with these exact values: 5, 12, 7, 20, 3.
R Programming
Need a hint?

Use the c() function to create a vector with the given numbers.

2
Set the threshold value
Create a variable called threshold and set it to the number 10.
R Programming
Need a hint?

Just assign the number 10 to the variable threshold.

3
Calculate sum with debugging
Write a for loop using the variable num to go through each element in numbers. Inside the loop, use print(num) to show the current number, then use if to check if num is greater than threshold. If yes, add num to a variable called total. Use browser() inside the if block to pause execution for debugging. Initialize total to 0 before the loop.
R Programming
Need a hint?

Remember to initialize total before the loop. Use for (num in numbers) to loop. Use print(num) to see each number. Use browser() inside the if block to pause.

4
Print the final sum
Write a print() statement to display the value of total after the loop.
R Programming
Need a hint?

Use print(total) to show the final sum.