# Creating a vector of numbers
numbers <- c(1, 2, 3, 4, 5)
# Using a for loop to iterate over the elements
for (num in numbers) {
# Printing each element
print(num)
}
In this example, we have a vector called numbers that contains five elements. The for loop iterates over each element in the numbers vector, assigns it to the variable num, and then executes the code block enclosed within the curly braces. In this case, it simply prints each number on a separate line.
Certainly! Here's an example of a for loop in R:
In this example, we have a vector called
numbers
that contains five elements. The for loop iterates over each element in thenumbers
vector, assigns it to the variablenum
, and then executes the code block enclosed within the curly braces. In this case, it simply prints each number on a separate line.