[英]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.1
, eq1.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.1
和eq1.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)
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.