列表中的rbind数据框 [英] rbind dataframes in a list of lists

查看:84
本文介绍了列表中的rbind数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,如下所示: x [[state]] [[year]] 。它的每个元素都是一个数据帧,并且单独访问它们不是问题。

I have a list of lists that looks like this: x[[state]][[year]]. Each element of this is a data frame, and accessing them individually is not a problem.

但是,我想在多个列表中对数据帧进行rbind。更具体地说,我想要像过去那样拥有尽可能多的数据帧的输出,这就是在每年内rbind所有的状态数据帧。换句话说,我想将所有的国家数据逐年合并到单独的数据框架中。

However, I'd like to rbind data frames across multiple lists. More specifically, I'd like to have as output as many dataframes as I have years, that is rbind all the state data frames within each year. In other words, I'd like to combine all my state data, year by year, into separate data frames.

我知道我可以将单个列表合并成一个数据框与 do.call(rbind,list)。但是我不知道如何在列表列表中执行此操作。

I know that I can combine a single list into a data frame with do.call("rbind",list). But I don't know how I can do so across lists of lists.

推荐答案

我无法测试,因为我没有这样的结构):

You can do something along the following lines (I could not test as I have no such structure):

extract.year <- function(my.year) lapply(x, function(y) y[[my.year]])

x.by.year <- sapply(my.list.of.years, function(my.year)
    do.call(rbind, extract.year(my.year)))   

函数提取年份创建一个仅包含给定年份的数据框的列表。然后你把它们绑在一起...

The function extract year creates a list containing just the dataframes for the given year. Then you rbind them...

这篇关于列表中的rbind数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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