Exercise 1: Basic Arithmetic
Calculate the sum of 15 and 30.
Solution:
Hint
Use the +
operator to add two numbers like a machine calculator.
Solution:
15 + 30
Exercise 2: Variable Assignment
Assign the value 100 to a variable named my_variable
and print it.
Hint
Use the <-
(or =
) operator to assign a value to a variable.
Solution:
# Assign value to a variable
<- 100
my_variable
# or
= 100
my_variable
# Print the variable
my_variable
Exercise 3: Creating a Vector
Create a vector named my_vector
containing the numbers 1, 2, 3, 4, and 5.
Hint
Use the c()
function to create a vector.
Tips
Alternatively use the :
function to create a vector.
Solution:
# Create a vector
c(1, 2, 3, 4, 5)
# or
1:5
Exercise 4: Basic Function Usage
Find the mean of the numbers 10, 20, 30, 40, and 50.
Hint
Use the mean()
function to calculate the average of a set of numbers.
Solution:
# Calculate the mean
<- c(10, 20, 30, 40, 50)
numbers mean(numbers)
Exercise 5: Conditional Statement
Write a conditional statement that checks if a number x
is greater than 10. If it is, print “Greater than 10”; otherwise, print “10 or less”.
Hint
Use an if
statement to perform a conditional check.
Tip
Solution:
<- 12
x
# Conditional statement
if (x > 10) {
print("Greater than 10")
else {
} print("10 or less")
}