0

I want to create a several dataframes in a loop (in R) And the name of each dataframe consists of a name + loop index. For example:

B1, B2, B3, ...,B10 1 until 10 are loop indexes

Now, i want to access these dataframes that is, when calling Bi, it will show its contents. For example:

for (i in 1:10) {
compare (Bi $ label, test $ label)
}

I've run the following code in R, but in the next steps I can not use dataframes

     > for(i in 1:4){
+ df.name<-paste("B",i)
+ df.name[i]<-i+1
+ print(df.name[i])}

How can I do this? Thanks for your help

maria
  • 65
  • 5
  • 5
    you would need to use `assign` but it's a bad idea, create a list of `data.frames` instead – Moody_Mudskipper Oct 06 '17 at 19:47
  • Does any of [these options](https://www.google.no/search?q=r+loop+list+data.frame&gws_rd=cr&dcr=0&ei=KN7XWf_PBYv36ASN2aWwDQ) help? – AkselA Oct 06 '17 at 19:50
  • Make a [list of data frames (click for link)](https://stackoverflow.com/a/24376207/903061), your list can be named `B` and you can access `B[[1]]`, `B[[7]]`, etc. – Gregor Thomas Oct 06 '17 at 19:54

1 Answers1

0

Maybe you would make a list of data.frames. Is safer than create a lot of variables with data.frames inside, and much more organized.

ls<-list()

Thus, you can store date.frames in the list, and call them when you need.

ls[1]<-df1
ls[2]<-df2
.
.
.
ls[x]<-dfx

#Calling one of the data.frames in the list
>ls[3]
  # Will show the third data.frame that you put there.

If you have some way to generate multiple dataframes, you can store them in this list, using a loop, as you described.

for(i in 1:number of data.frames){
   ls[i]<-df
}

I hope that helps you.

Regards.

Luis Antolin
  • 136
  • 7