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 7184817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:16:45+00:00 2026-05-28T18:16:45+00:00

What is the best way to re-write this code into a loop? a.data1 <-

  • 0

What is the best way to re-write this code into a loop?

a.data1 <- read.csv('outdata1.csv')
growth.sub.QOG1 <- merge(QOG, a.data1, by = c('year', 'country'), all = F)
growth.re1 <- plm(NY.GDP.PCAP.KD.ZG ~ log(Enrolment.in.all.programmes..Tertiary..Total)    + law + engineering + log(SP.POP.TOTL) + lp.legor
 ,data=growth.sub.QOG1, model="random")
summary(growth.re1)
eststo(growth.re1)


a.data2 <- read.csv('outdata2.csv')
growth.sub.QOG2 <- merge(QOG, a.data2, by = c('year', 'country'), all = F)
growth.re2 <- plm(NY.GDP.PCAP.KD.ZG ~ log(Enrolment.in.all.programmes..Tertiary..Total) + law + 
                  engineering + log(SP.POP.TOTL) + lp.legor
                    ,data=growth.sub.QOG2, model="random")
summary(growth.re2)
eststo(growth.re2)

a.data3 <- read.csv('outdata3.csv')
growth.sub.QOG3 <- merge(QOG, a.data3, by = c('year', 'country'), all = F)
growth.re3 <- plm(NY.GDP.PCAP.KD.ZG ~ log(Enrolment.in.all.programmes..Tertiary..Total) + law + 
                  engineering + log(SP.POP.TOTL) + lp.legor
                    ,data=growth.sub.QOG3, model="random")
summary(growth.re3)
eststo(growth.re3)

I tried to do something like this:

for (i  in 1:10) {
a.data[i] <- read.csv('outdata[i].csv')
growth.sub.QOG[i] <- merge(QOG, a.data[i], by = c('year', 'country'), all = F)
growth.re[i] <- plm(NY.GDP.PCAP.KD.ZG ~ log(Enrolment.in.all.programmes..Tertiary..Total) + law + 
                  engineering + log(SP.POP.TOTL) + lp.legor
                    ,data=growth.sub.QOG[i], model="random")
summary(growth.re[i])
eststo(growth.re[i])
}

but it didn’t work, what is it that I’m doing wrong?

  • 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-05-28T18:16:46+00:00Added an answer on May 28, 2026 at 6:16 pm

    Construct your file names.

    files <- paste("outdata", 1:3, ".csv", sep = "")
    #alternatively, use list.files/dir as suggested by Chris
    

    How you structure the rest of your code depends upon whether or not you care about those intermediate variables. I’ve assumed that you do, so you have lots of separate loops. If you don’t care, merge the lapply statements.

    Read in the data.

    all_data <- lapply(file, read.csv)
    

    Merge.

    merged <- lapply(all_data, function(data) 
    {
      merge(QOG, data, by = c('year', 'country'), all = FALSE)
    })
    

    Model.

    models <- lapply(merged, function(data)
    {
      plm(
        NY.GDP.PCAP.KD.ZG ~ log(Enrolment.in.all.programmes..Tertiary..Total) + law + engineering + log(SP.POP.TOTL) + lp.legor,
        data, 
        model = "random"
      )
    })
    

    Display some output.

    (summaries <- lapply(models, summary))
    (eststos <- lapply(models, eststo))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wanted to ask about the best way to write this JQUERY code. I
Following on from this question what would be the best way to write a
Whats the best way to read/write file properties (like author, description, etc) in C#?
What is the best way to write data to the text console at arbitrary
What's the best way to write a LINQ query that inserts a record and
What is the best way to write bytes in the middle of a file
What's the best way to write a supybot plugin in ruby? Or other language
What is the best way to write a function (or something DSLish) that will
What's the best way to write an application in order to get it working
What would be the best way to write log statements to a file or

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.