VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-do-i-rename-a-data-frame-in-a-for-loop-in-r/

⇱ How Do I Rename a Data Frame in a For Loop in R? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How Do I Rename a Data Frame in a For Loop in R?

Last Updated : 23 Jul, 2025

When working with multiple data frames in R, there are scenarios where you might want to rename data frames dynamically within a loop. This is particularly useful in situations where you're reading or generating several data frames programmatically and need to assign them meaningful names.

Why Rename Data Frames Dynamically?

Renaming data frames dynamically can be essential in various situations, such as:

  • Processing Multiple Files: When reading multiple files into separate data frames and needing each data frame to have a name related to its content.
  • Generating Multiple Data Frames: When performing operations in a loop that produce multiple data frames, each requiring a distinct name.

Understanding Object Naming in R

In R Programming Language managing object names dynamically can be tricky because R treats object names as symbols, not as character strings. However, with the right techniques, you can rename data frames or create new ones with dynamic names inside a loop.

Example 1: Basic Renaming of Data Frames in a For Loop

Suppose you have a list of data frames, and you want to rename each data frame with a prefix and a numeric suffix that corresponds to the loop index.

Output:

[1] "df_1" "df_2" "df_3" "df_list" "df_name"
  • assign(new_name, df_list[[i]]) creates new data frames named df_1, df_2, and df_3 in the global environment.
  • ls(pattern = "df_") lists all variables in the global environment that start with df_, confirming the creation of the new data frames.

Example 2: Using get() and assign() for Advanced Manipulation

You can also combine get() and assign() for scenarios where you need to modify or interact with the dynamically named data frames within the loop.

Output:


A B new_col
1 1.5277077 0.3556636 1.8833713
2 -1.0088882 0.0534445 -0.9554437
3 0.1253782 -0.5564637 -0.4310855
4 0.2512522 -1.0467175 -0.7954652
5 0.4748035 0.5372159 1.0120194


A B new_col
1 -1.3657994 0.08980049 -1.2759989
2 -0.5604294 0.19802429 -0.3624051
3 0.2237632 -0.29152582 -0.0677626
4 -1.3989924 -0.49646713 -1.8954595
5 1.7557527 -0.27659726 1.4791554


A B new_col
1 0.1021396 0.4867544 0.5888940
2 -0.5360342 -1.5772886 -2.1133228
3 -1.3588374 0.2909607 -1.0678767
4 0.1427831 1.1311762 1.2739593
5 -1.3799172 -0.1327074 -1.5126246

Example 3: Renaming and Modifying Data Frames Inside the Loop

If you want to not only rename but also modify the data frames inside the loop, you can do so before assigning the new name.

Output:

[1] "modified_df_1" "modified_df_2" "modified_df_3"

A B C
1 1 4 5
2 2 5 7
3 3 6 9

A B C
1 7 10 17
2 8 11 19
3 9 12 21

A B C
1 13 16 29
2 14 17 31
3 15 18 33
  • Inside the loop, each data frame is modified by adding a new column C before it is assigned a new name.
  • The newly created data frames modified_df_1, modified_df_2, and modified_df_3 now include the new column.

Conclusion

Renaming or creating data frames dynamically within a loop in R can be achieved using several methods, each suited to different use cases. The assign() function is straightforward and ideal for simple scenarios, while list2env() and eval() provide more flexibility for complex operations. Understanding these methods allows you to manage multiple data frames effectively, improving the scalability and organization of your R code.

Comment
Article Tags:

Explore