tpemartin / 112-1-programming-for-data-science

MIT License
1 stars 6 forks source link

Give me an example of for loop in R #10

Open tpemartin opened 7 months ago

tpemartin commented 7 months ago

Certainly! Here's an example of a for loop in R:

# 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.