Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

R Nested Loop


Sure! Nested for loops in R are like having a set of tasks to do, and for each task, you have another set of tasks to do. It's like a task within a task.

Let's break it down with an example where we want to print a multiplication table:

Example

# Let's say we want to print a multiplication table from 1 to 5 using nested for loops

# Outer loop for rows
for (i in 1:5) {
# Inner loop for columns
for (j in 1:5) {
# Multiply i by j and print the result
result <- i * j
cat(result, "\t") # Use cat() to print without a newline
}
cat("\n") # Move to the next line after each row
}

# After the loops finish, we can do other stuff
print("Multiplication table printed!")

In this example:

That's how nested for loops work in R! They're useful when you need to perform a task for every combination of elements in two or more sets.


Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java