Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r:循环遍历列表列表并将结果附加到数据帧_R_List_For Loop - Fatal编程技术网

r:循环遍历列表列表并将结果附加到数据帧

r:循环遍历列表列表并将结果附加到数据帧,r,list,for-loop,R,List,For Loop,我有以下列表,list1和list2: 库(tidyverse) df1a#A tible:2 x 4 #>索引第一列第二列类型 #> #>1 3 7 df1b #>2 6 df1b 然后我可以将结果放入一个数据帧中: all1#A tible:4 x 4 #>索引第一列第二列类型 #> * #>1.5.5 df1a #>2.3.5 7.5 df1a #>3137DF1B #>4 2 6 df1b 但是,如果我想

我有以下列表,
list1
list2

库(tidyverse)
df1a#A tible:2 x 4
#>索引第一列第二列类型
#>                     
#>1 3 7 df1b
#>2 6 df1b
然后我可以将结果放入一个数据帧中:

all1#A tible:4 x 4
#>索引第一列第二列类型
#> *                   
#>1.5.5 df1a
#>2.3.5 7.5 df1a
#>3137DF1B
#>4 2 6 df1b
但是,如果我想将
list1
list2
放入
大列表中,并循环遍历它,该怎么办?
这就是我尝试过的:

big_列表2 1.5 5.5 df2a
#>3 1 5 3.5 df2b
#>4.2.5.4 df2b
我尝试了许多不同的方法,但我无法将四个数据帧附加到一个4×8的数据帧中


由(v2.0.0)于2021-05-06创建,我们可以用
长度
初始化'y',与
大列表的
长度
相同,循环“大列表”的序列,(也可以用内部列表的
长度
初始化'x'

y <- vector('list', length(big_list))
for (j in seq_along(big_list)){
  x <- list()
    for (i in seq_along(big_list[[j]])) {
       ps <- output_mean(big_list[[j]][[i]], names(big_list[[j]])[i])
       x[[i]] <- ps
      }

  y[[j]]  <- do.call(rbind, x)
}

 out <- do.call(rbind, y)
-输出

out
# A tibble: 8 x 4
#  index first_column second_column type 
#  <dbl>        <dbl>         <dbl> <chr>
#1     1          1.5           5.5 df1a 
#2     2          3.5           7.5 df1a 
#3     1          3             7   df1b 
#4     2          2             6   df1b 
#5     1          3.5           7.5 df2a 
#6     2          1.5           5.5 df2a 
#7     1          5             3.5 df2b 
#8     2          5.5           4   df2b 
out1
# A tibble: 8 x 4
#  index first_column second_column type 
#  <dbl>        <dbl>         <dbl> <chr>
#1     1          1.5           5.5 df1a 
#2     2          3.5           7.5 df1a 
#3     1          3             7   df1b 
#4     2          2             6   df1b 
#5     1          3.5           7.5 df2a 
#6     2          1.5           5.5 df2a 
#7     1          5             3.5 df2b 
#8     2          5.5           4   df2b 
out1
#一个tibble:8x4
#索引第一列第二列类型
#                    
#1.5.5 df1a
#2.3.5 7.5 df1a
#3137DF1B
#4 2 6 df1b
#5 1 3.5 7.5 df2a
#6.2.5.5 df2a
#7 1 5 3.5 df2b
#8.2.5.4 df2b

我建议将
大列表
存储为串联列表,而不是嵌套列表

big_list <- c(list1, list2)
和基准R:

do.call(rbind, Map(output_mean, big_list, names(big_list)))

谢谢,但是正如您所看到的,
type
应该是数据帧的名称(
df1a
df1b
等等)所有的数据帧都应该附加在一个4×8的数据帧中。如果我不清楚的话,很抱歉。@Emy你的意思是将输出作为最新的数据帧,我希望它们组合在一个数据帧中。谢谢你的帮助。@Emy我添加了一个更紧凑的版本,带有
map/imap
看起来很棒,谢谢!有任何关于学习更多数据帧的建议吗关于
列表
map/imap
y <- list()
for (i in names(big_list)) {
    ps <- output_mean(big_list[[i]], i)
    y[[paste0(i)]] <- ps
}

all = do.call(rbind, y)
all

#  index first_column second_column type 
#*  <dbl>        <dbl>         <dbl> <chr>
#1     1          1.5           5.5 df1a 
#2     2          3.5           7.5 df1a 
#3     1          3             7   df1b 
#4     2          2             6   df1b 
#5     1          3.5           7.5 df2a 
#6     2          1.5           5.5 df2a 
#7     1          5             3.5 df2b 
#8     2          5.5           4   df2b 
purrr::imap_dfr(big_list, output_mean)
do.call(rbind, Map(output_mean, big_list, names(big_list)))