The for() function in R

for() function in r

Our tutorial will introduce the for() function – an essential function in R and other programming languages that helps traverse iterable objects.

Let’s move on to discover all about the function. 

What is the for() function in R?

Like other programming languages, the for() function in R is used to traverse the iterable objects and access each element to execute the expression in each step. 

Syntax: 

for ( value in iterable_object)
{
	block_of_commands
}

Parameters: 

  • value: Value of element in each step of the loop.
  • iterable_object: The iterable object used to run the loop. The object can be a list, a vector, an array, etc…
  • block_of_commands: A sequence of expressions that execute their function at each stage of the loop.

How to use the for() function?

Use the function to loop an interval

Syntax: 

for ( value in begin:end)
{
	block_of_commands
}

We will traverse an interval from 1 to 10 and print out the result:

for (i in 1:10){ 
    # Print results within one line
    cat(paste(" ", i))
}

Result:

1 2 3 4 5 6 7 8 9 10

In the example above, we use the for() function to loop from 1 to 10 assigned 1:10, character “i” symbols value of each number during the loop. To void printing value in many lines, we combine the cat() function and the paste() function to show all the results in one line. 

Use the function to loop an interval with a fixed step

Syntax:

for ( value in seq(begin, end, step)
{
	block_of_commands
}

To have a step of the loop, pass the seq() function as an interval. The seq() function defines an interval with a start point, endpoint, and step. 

Code:

for (i in seq(10, 100, by = 10)){
    cat(paste(" ", i))
}

Result:

10 20 30 40 50 60 70 80 90 100

Use the function to loop a list

Syntax:

for ( value in list)
{
	block_of_commands
}

Code:

myList = list(22, 3.14, "string", c(1, 2, 3, 4), TRUE)
print("The list is")

for (i in myList){
    cat(paste(" ", i))
}

Result:

"The list is"
22  3.14  string  1   2   3   4  TRUE

Use the function to loop characters of a string

In R, a string is considered a character vector with a length equal to 1. To loop the string, you must split it into a vector with the length n. To split a string, use the strsplit() function. After that, we can use the for() loop with the new string.

Code:

myString <- "Learn Share IT"
 
splitString <- strsplit(myString, "")[[1]]
 
for (i in splitString){
    cat(paste(i, " "))
}

Result:

L  e  a  r  n     S  h  a  r  e     I  T 

Use the function to loop a column of a data frame

To loop a data frame’s column, pass the column as the iterable object.

Code:

color <- c("Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet")
index <- c(1, 2, 3, 4, 5, 6, 7)
 
# Create a data frame from 2 vectors above
rainbow = data.frame(index, color)

# Print out values of the column color
for (i in rainbow$color){
    cat(paste(i, " "))
}

Result:

Red  Orange  Yellow  Green  Blue  Indigo  Violet

Summary

In summary, for() function in R is a function to traverse iterable objects and plays an important part in all programming languages. In this article, we introduce you to use the function with the popular data structure in R. We hope you gain much more knowledge from this tutorial.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *