📖 Scenario: You want to learn how to run simple R code to do basic calculations and see the results.
🎯 Goal: Write and run R code step-by-step to create variables, perform calculations, and print the results.
📋 What You'll Learn
Create variables with exact names and values
Use arithmetic operations in R
Print output using print()
💡 Why This Matters
🌍 Real World
Running R code is the first step to analyzing data, creating reports, and building statistical models.
💼 Career
Many jobs in data science, statistics, and research require running R code to process and understand data.
Progress0 / 4 steps
1
Create variables
Create a variable called a and set it to 10. Create another variable called b and set it to 5.
R Programming
Hint
Use the assignment operator <- to assign values to variables in R.
2
Calculate sum
Create a variable called sum_ab that stores the sum of a and b.
R Programming
Hint
Use the + operator to add two variables.
3
Calculate product
Create a variable called product_ab that stores the product of a and b.
R Programming
Hint
Use the * operator to multiply two variables.
4
Print results
Use print() to display the values of sum_ab and product_ab.
R Programming
Hint
Use print(variable_name) to show the value of a variable.
Practice
(1/5)
1. What does running R code mean?
easy
A. Installing R software
B. Only writing comments in the script
C. Saving files without executing them
D. Typing commands or running script files to perform tasks
Solution
Step 1: Understand the meaning of running code
Running code means executing commands or scripts to perform actions in R.
Step 2: Identify the correct description
Typing commands or running script files matches the definition of running R code.
Final Answer:
Typing commands or running script files to perform tasks -> Option D
Quick Check:
Running code = executing commands/scripts [OK]
Hint: Running code means executing commands or scripts [OK]
Common Mistakes:
Confusing running code with just writing comments
Thinking saving files runs code
Mixing running code with installing software
2. Which of the following is the correct way to run an R script named script.R from the console?
easy
A. source('script.R')
B. run('script.R')
C. execute('script.R')
D. load('script.R')
Solution
Step 1: Recall the function to run scripts in R
The function source() is used to run R scripts from the console.
Step 2: Match the correct syntax
source('script.R') correctly runs the script named script.R.
Final Answer:
source('script.R') -> Option A
Quick Check:
Run script = source() [OK]
Hint: Use source() to run R scripts from console [OK]
Common Mistakes:
Using run() which is not an R function
Using execute() which does not exist in R
Using load() which loads data, not scripts
3. What will be the output of the following R code?
print(2 + 3 * 4)
medium
A. 14
B. 20
C. 24
D. Error
Solution
Step 1: Understand operator precedence in R
Multiplication (*) has higher precedence than addition (+), so 3 * 4 is calculated first.
Step 2: Calculate the expression
3 * 4 = 12, then 2 + 12 = 14.
Final Answer:
14 -> Option A
Quick Check:
2 + (3 * 4) = 14 [OK]
Hint: Multiply before adding in R expressions [OK]
Common Mistakes:
Adding before multiplying ignoring precedence
Expecting error due to syntax
Confusing multiplication with addition
4. Identify the error in the following R code snippet:
print('Hello World)
medium
A. Missing closing parenthesis for print
B. Missing closing quote for the string
C. print() is not a valid function in R
D. No error, code runs fine
Solution
Step 1: Check string syntax
The string starts with a single quote but does not have a matching closing quote.
Step 2: Confirm the error type
This causes a syntax error because the string is not properly closed.
Final Answer:
Missing closing quote for the string -> Option B
Quick Check:
Unclosed string = syntax error [OK]
Hint: Always close quotes around strings [OK]
Common Mistakes:
Thinking print() is invalid in R
Ignoring missing quote and blaming parentheses
Assuming code runs without errors
5. You want to run multiple R commands stored in a file named analysis.R. Which is the best way to run all commands at once and see their output in the console?
hard
A. Open the file and run each line manually
B. Copy and paste the file content into the console line by line
C. Use source('analysis.R') to run the whole script
D. Restart R and open the file without running it
Solution
Step 1: Understand how to run multiple commands in R
Running a whole script at once is done using the source() function.
Step 2: Identify the best method for running all commands and seeing output
source('analysis.R') runs all commands in the file and shows output in the console.
Final Answer:
Use source('analysis.R') to run the whole script -> Option C
Quick Check:
Run script = source() [OK]
Hint: Use source() to run entire R scripts at once [OK]
Common Mistakes:
Running lines manually wastes time
Copy-pasting line by line is inefficient
Restarting R does not run code automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.