Loop Through Columns and Rows of Data Frame in R

Working with data frames and loops is a common task in data analysis and manipulation in R. Loops can help you iterate through rows or columns of a data frame to perform various operations on the data

In this article, we will explore to loop through columns and rows of data frames in R using the popular mtcars data set. To load the data set, use the following command

Download Example File
data(mtcars)

The data set contains information about cars i.e. their weights, mpg, hp etc.

image-15

Given that we are going to use specific functions from tidyverse package, we need to load the package by using the command given below. If the package is not installed already, then both of the given commands should be executed.

install.packages(tidyverse) library(tidyverse)

Now, we want to create a new column in the mtcars data frame called “weight”. The already present variable by the name of “wt” contains data about the weight of cars in thousands of pounds. The new variable we are going to create should contain the weight of cars in units. For this purpose, we use the following command

mtcars <- mtcars %>% mutate(weight = NA)

Note that in the above command, the observations for the weight variable are missing observations represented by NA.

Loop Through Each Row of a Data Frame

Once the variable by the name of weight is created, the part where loops are useful in this process comes in. To create observations for the weight of cars in units, instead of thousands of pounds, we multiply the original column containing weights of cars in thousands of pounds “wt” by 1000. It would look like the following:

for(i in 1:nrow(mtcars)) {      mtcars$weight[i] <- mtcars$wt[i] * 1000 }

In the above command, the for loop iterates through each row of the data frame and performs this calculation, updating the “weight” column accordingly. The nrow() function is used to determine the number of rows in the “mtcars” dataset. Thus, it is used to define the range of values that the for loop will iterate over. The loop will run from 1 to the number of rows in the “mtcars” dataset, performing the specified operations for each row. This command effectively achieves the desired result of converting the weight values from thousands of pounds to unit pounds in the data frame.

Loop Through Each Column

In the previous command, we demonstrated how to iterate through each row of a data frame. Now, let’s explore the process of looping through each column in an R data frame. If you want to modify the column names in the mtcars data frame by adding a suffix to each name, this can also be done using a loop. However, the key difference is that in this case, we are iterating through each column within the data frame, rather than each row

To add a suffix, say car with each variable name, we can use the following command for mtcars data.

for (i in 1:ncol(mtcars)) {       colnames(mtcars)[i] <- paste0(colnames(mtcars)[i],"_car") }

Like nrow, the ncol() function is used to determine the number of columns in the “mtcars” dataset. It counts and returns the total number of columns in the dataset, which represents the number of variables present in the data set. This command will iterate through each column and change the column names as shown in the image below.

image-14

Thus, loops can be used to iterate through rows and columns of data frames in R, offering various tools for data exploration and manipulation.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Tweet
Share
Share
Pin