Certainly! Here's an example of a for loop in R that calculates the sum of numbers from 1 to 10:
sum <- 0 # Initialize sum variable
for (i in 1:10) {
sum <- sum + i # Add current value of i to the sum
}
print(sum) # Print the final sum
In this example, the for loop iterates over the sequence 1:10, and on each iteration, the value of i is added to the sum variable using the + operator. The loop will execute 10 times, with i taking on the values 1, 2, 3, ..., 10. After the loop completes, the final sum is printed, which in this case will be 55 (1 + 2 + 3 + ... + 10).