Home » How to Loop Through Column Names in R (With Examples)

How to Loop Through Column Names in R (With Examples)

by Tutor Aspire

Often you may want to loop through the column names of a data frame in R and perform some operation on each column. There are two common ways to do this:

Method 1: Use a For Loop

for (i in colnames(df)){
   some operation
}

Method 2: Use sapply()

sapply(df, some operation)

This tutorial shows an example of how to use each of these methods in practice.

Method 1: Use a For Loop

The following code shows how to loop through the column names of a data frame using a for loop and output the mean value of each column:

#create data frame
df #view data frame
df

  var1 var2 var3 var4
1    1    7    3    1
2    3    7    3    1
3    3    8    6    2
4    4    3    6    8
5    5    2    8    9

#loop through each column and print mean of column
for (i in colnames(df)){
    print(mean(df[[i]]))
}

[1] 3.2
[1] 5.4
[1] 5.2
[1] 4.2

Method 2: Use sapply()

The following code shows how to loop through the column names of a data frame using sapply() and output the mean value of each column:

#create data frame
df #view data frame
df

  var1 var2 var3 var4
1    1    7    3    1
2    3    7    3    1
3    3    8    6    2
4    4    3    6    8
5    5    2    8    9

#loop through each column and print mean of column
sapply(df, mean)

var1 var2 var3 var4 
 3.2  5.4  5.2  4.2 

Notice that the two methods return identical results.

Related: A Guide to apply(), lapply(), sapply(), and tapply() in R

You may also like