user9015164
user9015164

Reputation:

How to make a list of data.frame with a loop in R?

I am pretty new to R and I try to make my first loop for my school work.

My problem is a try to make a list of data.frame (Each iteration give a data.frame) but just the first data.frame show in my list

Exemple of my code :

Attribution=function(x){
list_of_frame <- replicate(10, data.frame())
  N=1
  while (N < 10)     {
  TIGE <- read.xlsx("E:PlacetteparPlacette.xlsx", N, colNames=T)

  ( some code)

   list_of_frame[[N]] <- TableauPlacette

  return(list_of_frame)

  } 
   N=N+1
    }

Result:
[[1]] = First Data.frame
[[2]] data frame with 0 columns and 0 rows
[[3]] data frame with 0 columns and 0 rows
[[4]] data frame with 0 columns and 0 rows

Sorry for my English is not my first language (I try my best ). I hope you understand my problem

Upvotes: 1

Views: 1918

Answers (1)

Dave2e
Dave2e

Reputation: 24069

You need to switch the N+1 and return statements around. As written the function is returning after the first iteration. Try this:

Attribution=function(x){
  list_of_frame <- replicate(10, data.frame())
  N=1
  while (N < 10)     {
    TIGE <- read.xlsx("E:PlacetteparPlacette.xlsx", N, colNames=T)

    ( some code)

    list_of_frame[[N]] <- TableauPlacette

    N<-N+1
  } 
  return(list_of_frame)
}

Upvotes: 2

Related Questions