将重叠data.frames列表转换为单个data.frame

时间:2015-06-09 14:35:30

标签: r dataframe plyr

我从列表中的多个同类群组中获得了一些人口信息。每个群组涵盖重叠的时间段。数据如下所示:

> raw.data
$`1`
  Year     Pop
1 1920 1927433
2 1921 1914551
3 1922 1900776

$`2`
  Year     Pop
1 1921 1915576
2 1922 1902075
3 1923 1887613

$`3`
  Year     Pop
1 1922 1902111
2 1923 1887862
3 1924 1872695

我想将其转换为单个数据框,其中列名称是年份,而对角线是人口数据。输出应如下所示:

> resulting.data
     1920    1921    1922    1923    1924
1 1927433 1915576 1902111      NA      NA
2      NA 1914551 1902075 1887862      NA
3      NA      NA 1900776 1887613 1872695

您可以在下面找到输入和所需输出的示例数据:

raw.data <- structure(list(`1` = structure(list(Year = 1920:1922, Pop = c(1927433L, 1914551L, 1900776L)), .Names = c("Year", "Pop"), row.names = c(NA, 3L), class = "data.frame"), `2` = structure(list(Year = 1921:1923, Pop = c(1915576L, 1902075L, 1887613L)), .Names = c("Year", "Pop"), row.names = c(NA, 3L), class = "data.frame"), `3` = structure(list(Year = 1922:1924, Pop = c(1902111L, 1887862L, 1872695L)), .Names = c("Year", "Pop"), row.names = c(NA, 3L), class = "data.frame")), .Names = c("1", "2", "3"))
resulting.data <- structure(list(X1920 = c(1927433, NA, NA), X1921 = c(1915576, 1914551, NA), X1922 = c(1902111, 1902075, 1900776), X1923 = c(NA, 1887862, 1887613), X1924 = c(NA, NA, 1872695)), .Names = c("X1920", "X1921", "X1922", "X1923", "X1924"), row.names = c(NA, -3L), class = "data.frame")

我看过this link提出了类似的问题,但我没有成功地根据自己的需要进行调整。我还尝试使用plyr首先尝试获得对角线,然后将它们组合起来然后我不确定如何进行组合。

1 个答案:

答案 0 :(得分:1)

do.call()rbind()一起使用,将数据转换为单个数据框,然后reshape2::dcast()进行重新整形:

dat <- do.call(rbind, raw.data)
dat$obs <- gsub(".*?\\.", "", row.names(dat))

library(reshape2)
dcast(dat, obs ~ Year, fun.aggregate = sum, value.var = "Pop")

     obs    1920    1921    1922    1923    1924
1   1 1927433 1915576 1902111       0       0
2   2       0 1914551 1902075 1887862       0
3   3       0       0 1900776 1887613 1872695