R: How to Iterate Over Rows of Data Frame


Often you may want to iterate over the rows of a data frame in R and perform some task on each row.

The easiest way to do so is by using the for() function from base R, which is designed to perform this exact task.

You can use the following basic syntax to use the for() function to iterate over the rows of a data frame and perform some task:

for( i in rownames(df) )
  print(df[i, "col1"])

This particular example iterates over the rows of the data frame named df and prints the values from the col1 column of the data frame for each row.

Note that you can perform any task that you’d like within the for() function but we used the print command to illustrate a simple example of a task you can do.

The following examples show how to iterate over the rows of a data frame in R in practice.

Note: The for() function comes built-in with R so you can use it without installing or loading any external packages.

Example: How to Iterate Over Rows of Data Frame in R

Suppose that we create the following data frame in R named df:

#create data frame
df <- data.frame(col1=c(1, 3, 3, 4, 6, 8, 12, 15, 19, 21),
                 col2=c(0, 0, 2, 3, 3, 4, 5, 5, 7, 8))
                    
#view data frame
df

   col1 col2
1     1    0
2     3    0
3     3    2
4     4    3
5     6    3
6     8    4
7    12    5
8    15    5
9    19    7
10   21    8

Now suppose that we would like to iterate over each row of the data frame and print the values from the col1 column of the data frame.

We can use the following syntax to do so:

#iterate over rows of data frame and print values from col1 column
for( i in rownames(df) )
  print(df[i, "col1"])

[1] 1
[1] 3
[1] 3
[1] 4
[1] 6
[1] 8
[1] 12
[1] 15
[1] 19
[1] 21

Notice that this syntax iterates over each row of the data frame and prints the value from the col1 column as the output.

Note that we can also perform more complex operations if we’d like.

For example, we can use the print command with the paste command to print text along with the value from the col1 column for each row:

#iterate over rows of data frame and print values
for( i in rownames(df) )
  print(paste("col1 value: ", df[i, "col1"]))

[1] "col1 value:  1"
[1] "col1 value:  3"
[1] "col1 value:  3"
[1] "col1 value:  4"
[1] "col1 value:  6"
[1] "col1 value:  8"
[1] "col1 value:  12"
[1] "col1 value:  15"
[1] "col1 value:  19"
[1] "col1 value:  21"

Notice that this prints each value from the col1 column of the data frame with the text “col1 value:” prepended to it.

It’s worth noting that the examples that we used here were with a data frame that only contained two columns but you can use similar syntax to iterate over the rows of a data frame with any number of columns.

Also note that the output produced in the for() function can be much more complex and involve operations on more than just one column of the data frame if you’d like.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Write a Repeat Loop in R
How to Append Values to a Vector Using a Loop in R
How to Create a Nested For Loop in R
How to Use While Loops in R

Leave a Reply

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