简体   繁体   English

在循环中创建几个数据帧(在R中)

[英]create a several dataframes in a loop (in R)

I want to create a several dataframes in a loop (in R) And the name of each dataframe consists of a name + loop index. 我想在一个循环中创建多个数据框(在R中),每个数据框的名称由一个名称+循环索引组成。 For example: 例如:

B1, B2, B3, ...,B10 1 until 10 are loop indexes B1,B2,B3,...,B10 1到10是循环索引

Now, i want to access these dataframes that is, when calling Bi, it will show its contents. 现在,我想访问这些数据帧,即在调用Bi时,它将显示其内容。 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 我已经在R中运行以下代码,但是在接下来的步骤中,我将无法使用数据框

     > 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 谢谢你的帮助

Maybe you would make a list of data.frames. 也许您会列出一个data.frames列表。 Is safer than create a lot of variables with data.frames inside, and much more organized. 比在内部创建多个具有data.frame的变量更安全,更井井有条的安全。

ls<-list()

Thus, you can store date.frames in the list, and call them when you need. 因此,您可以将date.frames存储在列表中,并在需要时调用它们。

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. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM