Posit Community

Using variables names in loops.

I am kind of you to R, I used to work on Stata, so it is kind to make the change.

I wish to run through variables using their names.

This actually doesn't work, since it is not taking the variable values but the variable as a name. On top of that I wish to create numerous mean variables using the number in the loop (for instance mean1 and mean2 in the present case). For those of you which are familiar with Stata, here is what I'm trying to do

forvalues x = 1/2 { gen mean x' = mean(a x') }

Thank you so much in advance!

The below code works:

However, there are other ways to achieve similar results.

I notice you've created a dataframe; this would also do what you want:

Or you could avoid loops altogether and use {dplyr} , which will retain your dataframe structure:

Thank you very very much! This is exactly what I needed.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.

r assign dataframe name in loop

Secure Your Spot in Our PCA Online Course - Register Until Feb. 29 (Click for More Info)

Joachim Schork Image Course

R Loop Through Data Frame Columns & Rows (4 Examples) | for-, while- & repeat-Loops

In this article you’ll learn how to loop over the variables and rows of a data matrix in the R programming language .

The article will consist of the following contents:

Let’s dive right in…

Example Data

As a first step, I’ll have to create some data that we can use in the examples later on:

Have a look at the previous output of the RStudio console. It reveals that our example data has five rows and three columns. All variables are numeric .

Example 1: for-Loop Through Columns of Data Frame

In this Example, I’ll illustrate how to use a for-loop to loop over the variables of a data frame. First, let’s store our data frame in a new data object:

Now, we can use the for-loop statement to loop through our data frame columns using the ncol function as shown below:

Let’s check how our data frame has changed:

As you can see based on the previous output of the RStudio console, we added +10 to each variable of our data frame.

Example 2: for-Loop Over Rows of Data Frame

It is also possible to apply for-loops to loop through the rows of a data frame. Example 2 explains how to use the nrow function for this task. First, let’s replicate our data:

Now, we can apply the following R code to loop over our data frame rows:

In this example, we have subtracted -100 from each cell of our data matrix:

Example 3: while-Loop Through Columns of Data Frame

This Example explains how to loop over the columns of a data frame using a while-loop . Again, we are first replicating our data:

Then, we also have to specify a running index that we can increase with each iteration of our while-loop:

Now, we can start running our while-loop:

The result of the previous R syntax looks as follows:

As you can see, we have added +100 to the first two columns of our data. The third column was kept as in the original input data, since the while-loop stopped at the second column.

Example 4: repeat-Loop Through Columns of Data Frame

Similar to while-loops, we can also use a repeat-loop to loop over the variables of a data frame. Again, we have to replicate our data…

…and we have to specify a running index:

Now, we can write and run a repeat-loop as shown below:

We have specified that our repeat-loop should break after the second iteration and, hence, only the first two variables where changed:

Video & Further Resources

Do you want to learn more about loops in R? Then you might have a look at the following video of my YouTube channel. I’m showing the examples of this article in the video:

Furthermore, you may want to read some of the related articles of my website. I have published numerous articles already:

  • for-Loop in R
  • while-Loop in R
  • repeat-Loop in R
  • The R Programming Language

Summary: In this R tutorial you learned how to loop through multiple columns and rows of a data table . Don’t hesitate to tell me about it in the comments section below, in case you have any additional questions.

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy .

26 Comments . Leave new

' src=

That was wonderful. My data.frame is like this. DATES <- rep(c("2021-01-18","2021-01-19"),each=5) STOCKDETAILS <- rep(c("MAJESCO","NSE","BUY",120000.00,"DEL","MASTEK","BSE","SELL",150000.00,"DEL"),times=1) I need to get dates only once, and the columns one by one. How to do that? Thanking you

I have solved the problem myself filtering the dataframe and using an inner for loop and outer for loop. That resolves my problem. though it may not sound the best solution in R

' src=

Hey Unnikrishnan,

Thanks for another comment! Glad to hear that you found a solution for your problem. Indeed, multiple nested for-loops are often not the best/fastest solution, but as long as you don’t have problems due to computation time that shouldn’t matter too much.

' src=

how to loop over 5 variable to find there summary statistic

Please excuse the delayed response, I just came back from holidays. Are you still looking for help?

Regards, Joachim

' src=

Hello, Joachim I have a doubt, wish that you could help me I want to do filter (with the tidyverse’s function) to each unique value (character) in a column (df$id_regiao) with the result I want to make a data frame to each unique character How can I do that with a loop ?

Could you illustrate what the desired output should look like? I’m afraid I don’t understand what you exactly want to do.

' src=

i’ve tried the while loop piece of code on the below data frame.

df var1 var2 var3 var4 1 21 27 23 21 2 23 27 23 21 3 23 28 26 22 4 24 23 26 28 5 15 12 18 19

code : i repeat { + df[,i ] <- df[,i] + 100 + i 2) + { break} + }

and I the below. df var1 var2 var3 var4 1 121 127 23 121 2 123 127 23 121 3 123 128 26 122 4 124 123 26 128 5 115 112 18 119

seems like it didnot work on 2nd column only. would you care to provide a reason.

thanks in advance

Hey Sitaram,

I just came back from vacation. Do you still need help with this question?

' src=

Dear Joachim I need help in making a list from many data.fram (data.fram1 to data.fram25) and need to make a list of them using for loop. I would be happy if you help me.

Hey Narges,

I’m sorry for the late response, I just came back from vacation. Do you still need help with your question?

' src=

Hi Joachim,

Thank you for the wonderful explanations on looping. I have a sample dataset shown below fruits taken by students in a week. I have similar data with 1000 observations and 10 different columns. I want to club my data as a total of: 1. Apple 2. Banana 3. Orange 4. Apple + banana 5. Apple + Orange 6. banana+ Orange 7. Apple + banana+ Orange

Could you kindly show me how to do it? Thank you

student_id Apple Banana orange 1 0 1 0 2 1 1 1 3 1 0 1 4 1 0 0 5 0 0 0 6 1 1 0 7 0 1 1 8 0 1 0

The data got distorted: id Apple Banana orange 1). 0 1 0 2). 1 1 1 3 ). 1 0 1 4 ). 1 0 0 5 ). 0 0 0 6 ). 1 1 0 7 ). 0 1 1 8 ). 0 1 0

Thanks for the kind comment. Please see my response to your question in the for-loop tutorial: https://statisticsglobe.com/for-loop-in-r

' src=

How do i do loop for every 2×2 matrix and get the inverse value ot it?

Could you please explain the structure of your input data and the desired output in some more detail?

' src=

I’m wondering if you can help me a problem?

I’m trying to iterate through a data frame using a for loop and identify negative values and multiply them by -10. I keep running into issues with the if condition I’m using to identify negative values. This is the code I’m using and the error that keeps occurring.

df <- data.frame(x = letters[1:5], y = c(-4,-2,0,2,4), z = c(3,4,-5,6,-8))

for(i in 2:ncol(df)){ if(i <0){ new_val <- df[,i]*-10 df[, i] <- new_val-df[ ,i] } }

Error msg: 'Error in if(df[,i] 1′

Any help on how to fix this or an alternative method would be greatly appreciated.

Hey Oriana,

I have just executed your code, and it worked without displaying any error messages.

Are you certain that this is precisely the code you had problems with?

' src=

I have almost the same code as Oriana (comment above). My code runs without error, but it doesn’t change my data frame column/s at all. I need to take all negative values make them positive and multiply by 100 i.e. multiply by -100.

for(i in 2:ncol(df)) { if(i < 0) { df$y <- df$y*-100 } }

Any help would be greatly appreciated. Thank you

' src=

Hello Jack,

Your case is a bit different than given in the tutorial. Try the following code out.

Let me know if you have any further questions.

Regards, Cansu

You are a saint! I managed to do it but I had three separate for loop codes. Not nice and neat like yours.

Thank you so much for your help

Thank you for your sweet words. I am glad that I could help 🙂

' src=

Hi, Joachim.

Is there a way to for-loop columns in a data frame with formulas.

Here’s an example:

Case Age Term Start Date 1 34 2 2018 2 21 3 2020 3 43 5 2015

Then, the additional columns should be Case — Y2015 Y2016 Y2017 Y2018 Y2019 Y2020 1 0 0 0 34 35 36 2 0 0 0 0 0 21 3 43 44 45 46 47 0

– bulet

I’m uncertain if I get your question correctly. Are you looking for something like this ?

' src=

great exercises. I got a lot of benefits from Youtube videos as well website. Thank you so much. Just wondering how do iterate through columns by data. e.g. if I have 1:N columns and want to iterate through columns using a specific date

Thank you so much for the kind words, glad you find my tutorials helpful!

I’m sorry for the delayed reply. I was on a long vacation, so unfortunately I wasn’t able to get back to you earlier. Do you still need help with your syntax?

Leave a Reply Cancel reply

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

Post Comment

Joachim Schork Statistician Programmer

I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.

Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe. I hate spam & you may opt out anytime: Privacy Policy .

PCA Course

Related Tutorials

for-Loop with Increments in R (Example) | Increment by Size / Step 2

for-Loop with Increments in R (Example) | Increment by Size / Step 2

Print Output of Loop in R (Example) | Return Inside of while- & for-Loops

Print Output of Loop in R (Example) | Return Inside of while- & for-Loops

Data Carpentry for Biologists

For loops in r, basic for loop.

  • Loops are the fundamental structure for repetition in programming
  • for loops perform the same action for each item in a list of things
  • To see an example of this let’s calculate masses from volumes using a loop
  • Need print() to display values inside a loop or function
  • Code in the loop will run once for each value in volumes
  • Everything between the curly brackets is executed each time through the loop
  • Code takes the first value from volumes and assigns it to volume and does the calculation and prints it
  • Then it takes the second value from volumes and assigns it to volume and does the calculation and prints it
  • So, this loop does the same exact thing as
Do Tasks 1 & 2 in Basic For Loops .

Looping with an index & storing results

  • R loops iterate over a series of values in a vector or other list like object
  • When we use that value directly this is called looping by value
  • But there is another way to loop, which is called looping by index
  • Looping by index loops over a list of integer index values, typically starting at 1
  • These integers are then used to access values in one or more vectors at the position inicated by the index
  • If we modified our previous loop to use an index it would look like this
  • We often use i to stand for “index” as the variable we update with each step through the loop
  • We then create a vector of position values starting at 1 (for the first value) and ending with the length of the object we are looping over
  • We don’t want to have to know the length of the vector and it might change in the future, so we’ll look it up using the length() function
  • Then inside the loop instead of doing the calculation on the index (which is just a number between 1 and 3 in our case)
  • We use square brackets and the index to get the appropriate value out of our vector
  • This gives us the same result, but it’s more complicated to understand
  • So why would we loop by index?

The advantage to looping by index is that it lets us do more complicated things

  • One of the most common things we use this for are storing the results we calculated in the loop
  • To do this we start by creating an empty object the same length as the results will be before the loop starts
  • To store results in a vector we use the function vector to create an empty vector of the right length
  • mode is the type of data we are going to store
  • length is the length of the vector
  • Then add each result in the right position in this vector
  • For each trip through the loop put the output into the empty vector at the i th position
  • Walk through iteration in debugger
Do Tasks 3-4 in Basic For Loops .
End of 1 hour class

Looping over multiple values

  • Looping with an index also allows us to access values from multiple vectors
Do Task 5 in Basic For Loops .

Looping with functions

  • It is common to combine loops with functions by calling one or more functions as a step in our loop
  • For example, let’s take the non-vectorized version of our est_mass function that returns an estimated mass if the volume > 5 and NA if it’s not.
  • We can’t pass the vector to the function and get back a vector of results because of the if statements
  • So let’s loop over the values
  • First we’ll create an empty vector to store the results
  • And them loop by index, calling the function for each value of volumes
  • This is the for loop equivalent of an mapply statement

Looping over data frames

  • By default when R loops over a data frame it loops over the columns
  • To loop over rows, loop by index and subset
  • If we want to use a specific column
Do Size Estimates By Name Loop .

Looping over files

  • Repeat same actions on many similar files
  • Let’s download some simulated satellite collar data
  • Now we need to get the names of each of the files we want to loop over
  • We do this using list.files()
  • If we run it without arguments it will give us the names of all files in the directory
  • But we just want the data files so we’ll add the optional pattern argument to only get the files that start with "locations-"
  • Once we have this list we can loop over it count the number of observations in each file
  • First create an empty vector to store those counts
  • Then write our loop
Do Task 1 of Multiple-file Analysis . Exercise uses different collar data

Storing loop results in a data frame

  • We often want to calculate multiple pieces of information in a loop making it useful to store results in things other than vectors
  • We can store them in a data frame instead by creating an empty data frame and storing the results in the i th row of the appropriate column
  • Associate the file name with the count
  • Also store the minimum latitude
  • Start by creating an empty data frame
  • Use the data.frame function
  • Provide one argument for each column
  • “Column Name” = “an empty vector of the correct type”
  • Now let’s modify our loop from last time
  • Instead of storing count in results[i] we need to first specify the count column using the $ : results$count[i]
  • We also want to store the filename, which is data_files[i]
Do Task 2 Multiple-file Analysis . Exercise uses different collar data

Subsetting Data (optional)

  • Loops can subset in ways that are difficult with things like group_by
  • Look at some data on trees from the National Ecological Observatory Network
  • Look at a north-south gradient in number of trees
  • Need to know number of trees in each band of y values
  • Use the grid lines which are 2.5 m
  • Then figure out the edges for each window
  • But we don’t want to go all the way to the far edge
  • Set up an empty data frame to store the output
  • Look over the left edges and subset the data occuring within each window

Nested Loops (optional)

  • Sometimes need to loop over multiple things in a coordinate fashion
  • Pass a window over some spatial data

Look at full spatial pattern not just east-west gradient

  • Basic nested loops work by putting one loop inside another one
  • Loop over x and y coordinates to create boxes
  • Need top and bottom edges
  • Redefine out storage

Sequence along (optional)

  • seq_along() generates a vector of numbers from 1 to length(volumes)

Statology

Statistics Made Easy

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

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

Method 2: use sapply().

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

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:

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:

Notice that the two methods return identical results.

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

' src=

Published by Zach

Leave a reply cancel reply.

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

  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R

Related Articles

  • Solve Coding Problems
  • Subset Dataframe Rows Based On Factor Levels in R
  • How to find maximum string length by column in R DataFrame ?
  • How to Combine Two Columns into One in R dataframe?
  • How to Find and Count Missing Values in R DataFrame
  • DataFrame Row Slice in R
  • How to Replace particular value in R dataframe ?
  • How to split a big dataframe into smaller ones in R?
  • How to Merge DataFrames Based on Multiple Columns in R?
  • Merge DataFrames by Row Names in R
  • How to assign column names based on existing row in R DataFrame ?
  • Split large R Dataframe into list of smaller Dataframes
  • Extract first N rows from dataframe in R
  • How to convert DataFrame column from Character to Numeric in R ?
  • Find rows which are not in other dataframe in R
  • Replace Blank by NA in R DataFrame
  • How to Stack DataFrame Columns in R?
  • Find the index of the maximum value in R DataFrame
  • Drop row(s) by number from given DataFrame in R
  • Select rows from R DataFrame that contain both positive and negative values

How to Loop Through Column Names in R dataframes?

In this article, we will discuss how to loop through column names in dataframe in R Programming Language.

Method 1: Using sapply()

Here we are using sapply() function with some functions to get column names. This function will return column names with some results

  • dataframe is the input dataframe
  • specific function is like mean,sum,min ,max etc

Example : R program to get column names in the dataframe by performing some operations

Method 2: Using colnames

By using this function we can get column names. We have to iterate through for loop to get all the column names.

  • iterator is a variable is used to iterate the elements

r assign dataframe name in loop

Please Login to comment...

author

  • R DataFrame-Programs
  • R-DataFrame
  • 10 Best ChatGPT Prompts for Lawyers 2024
  • What is Meta’s new V-JEPA model? [Explained]
  • What is Chaiverse & How it Works?
  • Top 10 Mailchimp Alternatives (Free) - 2024
  • Dev Scripter 2024 - Biggest Technical Writing Event By GeeksforGeeks

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

IMAGES

  1. R Create A Dataframe With Row Names

    r assign dataframe name in loop

  2. R Create A Dataframe With Row Names

    r assign dataframe name in loop

  3. DataFrame Operations in R

    r assign dataframe name in loop

  4. r

    r assign dataframe name in loop

  5. How to add dataframe to dataframe in R ?

    r assign dataframe name in loop

  6. Rename Rows in R Dataframe (With Examples)

    r assign dataframe name in loop

VIDEO

  1. Drop Multiple Columns from Data Frame Using dplyr Package in R (Example)

  2. convert pandas dataframe column to datetime

  3. pandas make a row the column names

  4. R : R

  5. Get Function Parameters as List in R (Example)

  6. pandas keep columns from dataframe

COMMENTS

  1. r

    How do I rename a data-frame in a for-loop Ask Question Asked 11 years, 2 months ago Viewed 65k times Part of Collective 22 I am very new to programming with R, but I am trying to read in multiple files for a directory and give them each a unique name.

  2. How do I assign a dataframe name dynamically

    merrittr: zonename<-data %>% filter (zone==numzone) assign (zonename,data %>% filter (zone==numzone) ) 1 Like Leon May 18, 2020, 2:43pm #3 I would highly recommend that you, rather than clutter your workspace with data frames, hold them in a list. You can do so by just a slight alteration to your code:

  3. Name Variables in for-Loop Dynamically in R (2 Examples)

    Example 1: Basic Application of assign Function Let's assume that we want to create a new variable containing the values of the first list element. This variable should be named variable_1. Then, we can apply the assign function as shown below: assign ("variable_1", my_list [[1]]) # Apply assign function variable_1 # 1 2 3 4 5

  4. How to Use a For-Loop in R (with 18 Code Examples)

    It is used to iterate over a collection of objects, such as a vector, a list, a matrix, or a dataframe, and apply the same set of operations on each item of a given data structure. We use for-loops to keep our code clean and avoid unnecessary repetition of a code block. The basic syntax of a for-loop in R is the following: for (variable in ...

  5. Produce a list of new dataframes in a for loop, then assign values

    This gets the names right, but there's something wrong with the assign function, the new dataframe only has its name within - so t_Aus_df only has Aus_df within it instead of the tranposed Aus_df data. Thank you for your time! AlexisW November 26, 2020, 5:25am #2 NuancedPaul: i in c ("Aus_df", "Canada_df", "US_df")

  6. Using loop variable names : r/rstats

    If I then try to bind that vector onto the pre-existing data frame though, we get a column literally called "items", with the variable held under it (bread, milk, etc) as a repeated value for all rows. In other instances using the word "items", such as when assigning it as the vector name, R recognises it just fine.

  7. Using variables names in loops

    Hi there, I am kind of you to R, I used to work on Stata, so it is kind to make the change. I wish to run through variables using their names. a1 = c(1,2,3,4,5,6) a2 = c(1,2,3,4,5,6) df = data.frame(a1,a2) for (i in 1:2) { variable = paste0("a", i) mean = mean(as.name(variable)) print(as.name(mean)) } This actually doesn't work, since it is not taking the variable values but the variable as a ...

  8. R Append to Data Frame in Loop (Example)

    Example 1: Add New Column to Data Frame in for-Loop In Example 1, I'll show how to append a new variable to a data frame in a for-loop in R. Have a look at the following R code: for( i in 1:3) { # Head of for-loop new <- rep ( i, nrow ( data)) # Create new column data [ , ncol ( data) + 1] <- new # Append new column

  9. R Loop Through Data Frame Columns & Rows (4 Examples)

    Now, we can apply the following R code to loop over our data frame rows: for( i in 1: nrow ( data2)) { # for-loop over rows data2 [ i, ] <- data2 [ i, ] - 100 } In this example, we have subtracted -100 from each cell of our data matrix:

  10. For Loops in R · Data Carpentry for Biologists

    Storing loop results in a data frame. We often want to calculate multiple pieces of information in a loop making it useful to store results in things other than vectors; We can store them in a data frame instead by creating an empty data frame and storing the results in the ith row of the appropriate column; Associate the file name with the count

  11. dataframe

    R dynamic data frame names in Loop Asked 6 years, 9 months ago Viewed 11k times Part of Collective 5 I have some df like df1 df2 df3 ... dfn So how to use my data frame using loop example: loop (i in 1:n) { summary (paste0 ("df",i)) # =====> It dose not work! } r dataframe dynamic Share Improve this question Follow asked May 22, 2017 at 10:47

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

    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)

  13. How to assign and dynamically change the name of a dataframe in R in a loop

    1 Answer Sorted by: 2 With the suggestion of Waldi I was able to solve it for (i in 1:nrow (data_s_y_revenue)) { name_y <- data_s_y_revenue$year [i] name_y1 <- paste ("data_y_y",name_y, sep="_") data_y_y <-filter (data_y, year==name_y) assign (name_y1, data_y_y) } Share

  14. How to Loop Through Column Names in R dataframes?

    Method 1: Using sapply () Here we are using sapply () function with some functions to get column names. This function will return column names with some results Syntax: sapply(dataframe,specific function) where dataframe is the input dataframe specific function is like mean,sum,min ,max etc

  15. How to assign name to data frames in a for loop? In R

    The for loop can be replaced with a sapply: dat<-sapply (name,function (x) assign (x,read.table (paste0 (x,".txt"),,sep="\t",header=TRUE)), USE.NAMES=TRUE, simplify=FALSE) This creates a single list (called "dat") with each element in it named using the elements of "name" and containing the table in the file (name).txt. Share.

  16. Generating variable names for dataframes based on the loop number in a

    Then, create your for loop and do the following to give the data frames the names you want: for (i in 1:10) { d.frame <- # create your data frame here assign (df.name [i], d.frame) } Now you will end up with ten data frames with ten different names. For your second question about the coefficients:

  17. in R function, assign data frame object as a name? loop through

    To answer your first question: you can obtain the name of an object x using deparse (substitute (x)). So to eliminate the argument obj_name from your function, you could use. draft_fxn <- function (x) { obj_name <- deparse (substitute (x)) x.selected<-x [,c (samplelist)] colnames (x.selected) [1:2]<-paste (obj_name, colnames (x.selected), sep ...

  18. dataframe

    dfnames <- c("d","e") scores.d <- data.frame(x = 1, y = 1:10) scores.e <- data.frame(x = 2, y = 11:20) vals <- 61:70 # for demonstrative purposes only, these were created in a loop in my code full.dfnames[1] <- "Scores.d" full.dfnames[2] <- "Scores.e" for (i in seq_along(dfnames)){ #added seq_along back for the name index dat<-get(full.dfnames ...

  19. How to change column names in dataframe in the loop?

    I have simplified your problem to three data frames with three columns, just to make creating the sample data easier. So these are the data frames: df1 <- data.frame(a = 1:10, b = 1:10, c = 1:10) df2 <- df1 df3 <- df1 And this is the loop that changes the names of the third columns: