然后从嵌套列表中提取row.bind data.frames [英] Extract then row.bind data.frames from nested lists

查看:109
本文介绍了然后从嵌套列表中提取row.bind data.frames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输出大矩阵(Mat1)和小数据帧(Smalldf1)的函数,我将它们存储在称为结果"的列表中. 此函数在循环内运行,因此我创建了结果"列表的许多版本,直到循环结束....每次将它们添加到名为最终"的列表中.

在循环结束时,我得到一个名为FINAL的列表,该列表内部有许多较小的结果"列表...每个列表都包含一个小数据框和一个大矩阵.

我想将所有小的数据帧捆绑在一起以形成一个较大的数据帧,并将其命名为DF1-现在我访问列表中的列表时,现在我不确定如何访问这些对象.?

此处类似的问题给出了这样的解决方案:

DF1 <- do.call("rbind",lapply(FINAL, function(x) x["Smalldf1"]))

但是,这将输出显示为一个名为"Smalldf1"的单列,并带有Smalldf1的描述...即按字面意思打印在列列表中(X1 ="xxx",X2 ="xxx",X3 ="xxx )...我需要将其分解为原始格式,其中包含3列信息...?

任何帮助都会很棒.

解决方案

我将我的评论写成答案.这可能是您的数据:

df <- data.frame(X1=1:3, X2=4:6, X3=7:9)
FINAL=list(Result=list(Smalldf1=df, Mat1=as.matrix(df)),
             Result=list(Smalldf1=df+1, Mat1=as.matrix(df+1)))

您可以结合使用lapply提取嵌套列表的第一个(或第N个,只需更改1)元素,然后使用do.call或dplyr对这个结果进行rbind:

#### # Doing it in base R:
do.call("rbind", lapply(FINAL, "[[", 1) )
#### # Or doing it with dplyr:
library(dplyr)
lapply(FINAL, "[[", 1) %>% bind_rows
####   X1 X2 X3
#### 1  1  4  7
#### 2  2  5  8
#### 3  3  6  9
#### 4  2  5  8
#### 5  3  6  9
#### 6  4  7 10

这应该是您的预期结果

警告: 使用dplyr的解决方案不适用于dplyr的旧版本(我在dplyr_0.5.0上进行了测试,但例如在dplyr_0.2上返回了错误)

I have a function that outputs a large matrix (Mat1) and a small dataframe (Smalldf1) I store them in a list called "Result". This function is run inside a loop so I create many versions of my "Result" list until my loop ends....each time I add them to a list called "FINAL".

At the end of my loop I end up with a List called FINAL, that has many smaller "Result" lists inside...each containing a small dataframe and a large Matrix.

I want to rbind all of the small dataframes together to form one larger dataframe and call it DF1 - I'm not sure how I access these now that I'm accessing a list within a list..?

A similar question on here gave a solution like this:

DF1 <- do.call("rbind",lapply(FINAL, function(x) x["Smalldf1"]))

However this gives the output as a single column called "Smalldf1" with the description of Smalldf1...ie this is literally printed in the column list(X1="xxx", X2="xxx", X3="xxx")...I need this broken out to look like the original format, 3 columns housing the information...?

Any help would be great.

解决方案

I make my comment into an answer. This could be your data:

df <- data.frame(X1=1:3, X2=4:6, X3=7:9)
FINAL=list(Result=list(Smalldf1=df, Mat1=as.matrix(df)),
             Result=list(Smalldf1=df+1, Mat1=as.matrix(df+1)))

You can combine lapply to extract the first (or Nth, just change the 1) elements of the nested lists, and then do a rbind on this result, either with do.call or with dplyr:

#### # Doing it in base R:
do.call("rbind", lapply(FINAL, "[[", 1) )
#### # Or doing it with dplyr:
library(dplyr)
lapply(FINAL, "[[", 1) %>% bind_rows
####   X1 X2 X3
#### 1  1  4  7
#### 2  2  5  8
#### 3  3  6  9
#### 4  2  5  8
#### 5  3  6  9
#### 6  4  7 10

This should be your expected result

WARNING: The solution using dplyr doesn't work for old versions of dplyr (i tested it on dplyr_0.5.0, but it returns an error on dplyr_0.2 for instance)

这篇关于然后从嵌套列表中提取row.bind data.frames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆