Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8434047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T06:33:06+00:00 2026-06-10T06:33:06+00:00

I’m trying to learn about loops and I currently have a long list of

  • 0

I’m trying to learn about loops and I currently have a long list of data frames and I need to go inside a bunch of these data frames and rename some variables. I have a function, but I’m struggling to construct a smart way to loop thru my list (the real list is much longer than in the example below) and at the same time apply varying suffixes prefixes hen renaming.

Hopefully my working example below will illustrate the situation. I imagine I can build the last part into two loops, but I can’t seem to figure out how I write to the data frame inside the list inside a loop.

Any help would be appreciated!

data(mtcars)

mtcarsList <- list(mtcars1 = mtcars, mtcars2 = mtcars,
                   mtcarsA = mtcars, mtcars = mtcars  )

# function I use to renames a specific number of variables
baRadd <- function(df, vector, suffix){
               names(df) <- ifelse(names(df) %in% vector,names(df), 
                                  paste(suffix, names(df), sep = ".")) 
               return(df)}


foo <- c("mpg", "cyl", "disp")
suffix1 <- "bar"
suffix2 <- "barBAR"
suffix3 <- "barBARbar"

mtcarsList$mtcars1 <- baRadd(mtcarsList$mtcars1, foo, suffix1)
mtcarsList$mtcars2 <- baRadd(mtcarsList$mtcars2, foo, suffix2)
mtcarsList$mtcarsA <- baRadd(mtcarsList$mtcarsA, foo, suffix3) 

 names(mtcarsList$mtcars1)
 # [1] "mpg"      "cyl"      "disp"     "bar.hp"   "bar.drat" "bar.wt"  
 # [7] "bar.qsec" "bar.vs"   "bar.am"   "bar.gear" "bar.carb"
 names(mtcarsList$mtcars2)
 # [1] "mpg"         "cyl"         "disp"        "barBAR.hp"   "barBAR.drat"
 # [6] "barBAR.wt"   "barBAR.qsec" "barBAR.vs"   "barBAR.am"   "barBAR.gear"
 # [11] "barBAR.carb"
 names(mtcarsList$mtcarsA)
 # [1] "mpg"            "cyl"            "disp"           "barBARbar.hp"  
 # [5] "barBARbar.drat" "barBARbar.wt"   "barBARbar.qsec" "barBARbar.vs"  
 # [9] "barBARbar.am"   "barBARbar.gear" "barBARbar.carb"
 names(mtcarsList$mtcars)
 # [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear"
 # [11] "carb"

Update,

Based on DWin’s response below I write this scrip that solves my issue,

# rm(list = ls(all = TRUE)) ## Clear workspace

data(mtcars)

mtcarsList <- list(mtcars1 = mtcars, mtcars2 = mtcars, 
                    mtcarsA = mtcars, mtcars = mtcars)

## function I use to renames a specific number of variables
baRadd <- function(df, vector, suffix){
               names(df) <- ifelse(names(df) %in% vector,names(df), 
                                  paste(suffix, names(df), sep = ".")) 
               return(df)}


suffixes <- c('A', 'B', 'C') # suffixes to be added to the three dfTO
whatNOTtoRename <- c("mpg", "cyl", "disp") 
# variables within the data frame I do not     want to renames
dfTO <- c('mtcars1','mtcars2','mtcarsA') 
# the specific data frames I need to rename


# str(mtcarsList)
mtcarsList[ names( mtcarsList[dfTO]) ] <- 
     mapply(baRadd, df=mtcarsList[dfTO], 
                    suffix= suffixes, 
                    MoreArgs=list(vector=whatNOTtoRename) , SIMPLIFY=FALSE)

str(mtcarsList)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T06:33:08+00:00Added an answer on June 10, 2026 at 6:33 am

    Looks as though mapply can do this task:

    > newList <- mapply(baRadd, df=mtcarsList[1:3], suffix= c(suffix1, suffix2, suffix3), MoreArgs=list(vector=foo) , SIMPLIFY=FALSE)
    > str(newList)
    List of 3
     $ mtcars1:'data.frame':    32 obs. of  11 variables:
      ..$ mpg     : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
      ..$ cyl     : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
      ..$ disp    : num [1:32] 160 160 108 258 360 ...
      ..$ bar.hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
      ..$ bar.drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
      ..$ bar.wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
      ..$ bar.qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
      ..$ bar.vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
      ..$ bar.am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
      ..$ bar.gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
      ..$ bar.carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
     $ mtcars2:'data.frame':    32 obs. of  11 variables:
      ..$ mpg        : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
      ..$ cyl        : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
      ..$ disp       : num [1:32] 160 160 108 258 360 ...
      ..$ barBAR.hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
      ..$ barBAR.drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
      ..$ barBAR.wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
      ..$ barBAR.qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
      ..$ barBAR.vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
      ..$ barBAR.am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
      ..$ barBAR.gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
      ..$ barBAR.carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
     $ mtcarsA:'data.frame':    32 obs. of  11 variables:
      ..$ mpg           : num [1:32] 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
      ..$ cyl           : num [1:32] 6 6 4 6 8 6 8 4 4 6 ...
      ..$ disp          : num [1:32] 160 160 108 258 360 ...
      ..$ barBARbar.hp  : num [1:32] 110 110 93 110 175 105 245 62 95 123 ...
      ..$ barBARbar.drat: num [1:32] 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
      ..$ barBARbar.wt  : num [1:32] 2.62 2.88 2.32 3.21 3.44 ...
      ..$ barBARbar.qsec: num [1:32] 16.5 17 18.6 19.4 17 ...
      ..$ barBARbar.vs  : num [1:32] 0 0 1 1 0 1 0 1 1 1 ...
      ..$ barBARbar.am  : num [1:32] 1 1 1 0 0 0 0 0 0 0 ...
      ..$ barBARbar.gear: num [1:32] 4 4 4 3 3 3 3 4 4 4 ...
      ..$ barBARbar.carb: num [1:32] 4 4 1 1 2 1 4 2 2 4 ...
    

    If you wanted to assign that result to mtcarsList[1:3], that too should be possible.

    To your comment: this succeeds ….

    mtcarsList[ names( mtcarsList[1:3]) ] <- 
             mapply(baRadd, df=mtcarsList[1:3], 
                            suffix= c(suffix1, suffix2, suffix3), 
                            MoreArgs=list(vector=foo) , SIMPLIFY=FALSE)
    
     # omitted output of str(mtcarsList)  ....
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I don't have much knowledge about the IPv6 protocol, so sorry if the question
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.