4.5 C
London
Thursday, December 19, 2024
HomeStatistics TutorialRHow to Loop Through Column Names in R (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories