# For Loop In R

For loop in R is just like any other language. The basic syntax is shown below.

for (val in sequence) {

```
Your R statements
```

}

Let us go through a simple example. Let us create a sequence of even numbers in R using seq function first.

```
even_seq = seq(2,10,by=2)
```

```
typeof(even_seq)
```

Let us for loop through above sequence.

```
for (n in even_seq)
print(n)
```

Note we don't need curly brackets, it is optional around the for loop. Here is an example with curly brackets.

```
for (n in even_seq) {
print(n)
}
```

Let us expand our example, check if number is even or not.

```
for (n in even_seq)
if ( n%%2==0)
print(paste(n," is even"))
else
print(paste(n, "is odd"))
```

However the brackets around if else loop is not optional. Let us put the curly brackets around "if else" and re-run our code.

```
for (n in even_seq)
if ( n%%2==0) {
print(paste(n," is even"))
} else {
print(paste(n, "is odd"))
}
```

## How to for loop through DataFrame in R

Let us first create a dataframe in R.

```
even_seq <- data.frame(even_numbers=seq(2,10,by=2))
```

Let us add a odd number too in our dataframe.

```
even_seq[6,] = 7
```

```
head(even_seq)
```

Let us loop through dataframe same way as we did above.

```
for (n in even_seq)
if (n%%2==0) {
paste(n,"is even")
} else {
paste(n, "is odd")
}
```

Oops looks like it didnt work. Let us remove the if else and just print numbers in the loop.

```
for (n in even_seq)
print(n)
```

Ok, as we see above instead of looping through each value, all the values are just printed on the same line. That is why if else loop didnt work as we expected.

```
typeof(even_seq$even_numbers)
```

So the correct way to loop through dataframe list of values is to use lapply function as shown below.

```
lapply(even_seq$even_numbers, function(x) {
if (x%%2==0) {
paste(x,"is even")
} else {
paste(x, "is odd")
}
})
```

Let me explain, what I did in the above code. I passed in the column 'even_number' to lapply R method. Second argument is the function that gets applied on each value. Therefore for each value in our column 'even_seq$even_numbers', function which checks whether the number is even or odd is called.

Let us make it more complex and create a data frame with two columns.

```
odd_even_seq <- data.frame(even_numbers = seq(2,10,by=2),odd_numbers=seq(1,10,by=2))
```

```
head(odd_even_seq,2)
```

Ok, we have dataframe with two columns. Let us loop through the dataframe and check if it is even or odd.

```
lapply(names(odd_even_seq), function(y) {
lapply(odd_even_seq[[y]],function(x) {
if (x%%2==0) {
paste(x,"is even")
} else {
paste(x, "is odd")
}
})
})
```

Note to make it work, we used two lapply functions. First one is to loop through the list of column names and then 2nd one is to loop through the list of values of each column.

## Wrap Up!

I hope you would find this tutorial useful.

#### Related Notebooks

- How to Generate Random Numbers in Python
- If Else Statement In R
- Boxplots In R
- Tidy Data In R
- How To Iterate Over Rows In A Dataframe In Pandas
- How To Use Grep In R
- How To Run Logistic Regression In R
- Regularization Techniques in Linear Regression With Python
- How to Create DataFrame in R Using Examples