简体   繁体   English

如何在单个数据帧中定义1000个数据帧?

[英]How to define 1000 dataframes in a single one?

My problem is the following. 我的问题如下。 Suppose I have 1000 dataframes in R with the names eq1.1 , eq1.2 , ..., eq1.1000 . 假设我在R中有1000个数据帧,其名称分别为eq1.1eq1.2 ,..., eq1.1000 I would like a single dataframe containing my 1000 dataframes. 我想要一个包含我的1000个数据帧的数据帧。 Normally, if I have only two dataframes, say eq1.1 and eq1.2 then I could define 通常,如果我只有两个数据帧,例如eq1.1eq1.2则可以定义

df <- data.frame(eq1.1,eq1.2)

and I'm good. 而且我很好。 However, I can't follow this procedure because I have 1000 dataframes. 但是,我无法执行此过程,因为我有1000个数据帧。

I was able to define a list containing the names of my 1000 dataframes using the code 我能够使用代码定义一个包含我的1000个数据框名称的列表

names <- c()
for (i in 1:1000){names[i]<- paste0("eq1.",i)} 

However, the elements of my list are recognized as strings and not as the dataframes that I previously defined. 但是,我列表中的元素被识别为字符串,而不是我先前定义的数据框。

Any help is appreciated! 任何帮助表示赞赏!

How about 怎么样

df.names <- ls(pattern = "^eq1\\.\\d")
eq1.dat <- do.call(cbind,
                   lapply(df.names,
                          get))
rm(list = df.names)
library(stringi)
library(dplyr)

# recreate dummy data #重新创建伪数据

lapply(1:1000,function(i){
      assign(sprintf("eq1.%s",i),
             as.data.frame(matrix(ncol = 12, nrow = 13, sample(1:15))),
             envir = .GlobalEnv)
    })

# Now have 1000 data frames in my working environment named eq1.[1:1000] #现在在我的工作环境eq1中有1000个数据帧。[1:1000]

> str(ls(pattern = "eq1.\\d+"))
> chr [1:1000] "eq1.1" "eq1.10" "eq1.100" "eq1.1000" "eq1.101" "eq1.102" "eq1.103" ...

1) create a holding data frame from the ep1.1 data frame that will be appended each iteration in the following loop 1)从ep1.1数据帧创建一个保持数据帧,该数据帧将在以下循环中附加到每个迭代中

empty_df <- eq1.1

2) im going to search for all the data frame named by convention and create a data frame from the returned characters which represent our data frame objects, but are nothing more than a character string. 2)我将搜索所有由约定命名的数据框,并从代表我们数据框对象的返回字符中创建一个数据框,但无非就是一个字符串。

3) mutate that data frame to hold an indexing column so that I can order the data frames properly from 1:1000 as the character representation will not be in numeric order from the step above 3)更改该数据框以保留索引列,以便我可以从1:1000正确排序数据框,因为从上述步骤开始,字符表示将不按数字顺序排列

4) Drop the indexing column once the data frame names are in proper sequence and then unlist the dfs column back into a character sequence and slice the first value out, since it is stored already to our empty_df 4)一旦数据帧名称按正确的顺序放置,就删除索引列,然后取消将dfs列重新列出为字符序列并切出第一个值,因为它已经存储在我们的empty_df中

5) loop through that sequence and for each iteration globally assign and bind the preceding data frame into place. 5)遍历该序列,并为每次迭代全局分配并绑定先前的数据帧。 So for example on iteration 1, the empty_df is now the same as data.frame(ep1.1, ep1.2) and for the second iteration the empty_df is the same as data.frame(ep1.1, ep1.2, ep1.3) 因此,例如在迭代1中,empty_df现在与data.frame(ep1.1,ep1.2)相同,而对于第二次迭代,empty_df与data.frame(ep1.1,ep1.2,ep1相同) .3)

NOTE: the get function takes the character representation and calls the data object from it. 注意: get函数采用字符表示形式并从中调用数据对象。 see ?get for details 请参阅?获取详细信息

lapply(
  data.frame(dfs = ls(pattern = 'eq1\\.\\d+'))%>% 
    mutate(nth = as.numeric(stri_extract_last_regex(dfs,'\\d+'))) %>%
    arrange(nth) %>% select(-nth) %>% slice(-1) %>% .$dfs, function(i){
      empty_df <<- data.frame(empty_df, get(i)) 
    }
)

All done, all the dataframes are bound to the empty_df and to check 全部完成,所有数据帧都绑定到empty_df并检查

 > dim(empty_df)
    [1]    13 12000

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

相关问题 如何制作一个循环以从 1000 个数据帧中获取一个变量 - how to make a loop to fetch one variable from 1000 dataframes 如何将R中的两个数据帧有效地合并为一个,重新计算每个新单元格的值(最好使用dplyr) - How to efficiently merge two dataframes in R, into a single one, recalculating the value for each new cell (preferrably using dplyr) 如何通过单个列合并数据帧列表? - How to merge a list of dataframes by a single column? 如何将列表中的数据帧写入单个csv文件 - How to write the dataframes in a list to a single csv file 如何将大小为25的1000个随机样本的代码更新为大小为25的一个随机样本的1000个重采样 - How to update code for 1000 random sample of size 25 to 1000 resamples of one random sample of size 25 如何在R中的一个列表中管理一组数据帧 - How to to manage a group of dataframes in one list in R 如何对数据框列表中的一列进行分组 - How to group by for one column within a list of dataframes 如何从现有数据框创建多个数据框? - How to create multiple dataframes from an existing one? 将多个数据帧的手段组合成一个数据帧,同时为每个数据帧显示一个名称 - Combining means of multiple dataframes into a single one while showing a name for each one 如何使用ggplot2从两个数据帧创建单个图 - How to make a single plot from two dataframes with ggplot2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM