首页 > 解决方案 > 如何组合具有相似结构和匹配命名元素的表列表?

问题描述

我有两个表列表,名为ObservedExpected其中包含具有相应名称的表和匹配的 r*2 表。

例如,在元素Observed列表中:AgeGroup

            0     1
  18-34 49841  8740
  0      4440  1687
  1-4   14537  2735
  13-17 11531  1656
  35-54 41580 11206
  5-12  17091  1798
  55-74 27807 16417
  75+   16068 22866

在元素Expected列表中:AgeGroup

                0         1
  18-34 42856.688 15724.312
  0      4482.391  1644.609
  1-4   12635.850  4636.150
  13-17  9647.345  3539.655
  35-54 38617.182 14168.818
  5-12  13818.815  5070.185
  55-74 32353.394 11870.606
  75+   28483.336 10450.664

我想创建一个新列表,它将两个列表中用于匹配元素的列组合在一起,所以新的元素表AgeGroup,比如在一个名为的新列表中CombinedOE,表(或者如果可能的话,数据框更好)看起来像这样.

在此处输入图像描述

预期的数字可能会四舍五入为整数。

数据样本:

Observed <- list(AgeGroup = structure(c(49841, 4440, 14537, 11531, 41580, 
17091, 27807, 16068, 8740, 1687, 2735, 1656, 11206, 1798, 16417, 
22866), .Dim = c(8L, 2L), .Dimnames = structure(list(c("18-34", 
"0", "1-4", "13-17", "35-54", "5-12", "55-74", "75+"), c("0", 
"1")), .Names = c("", "")), class = "table"), Sex = structure(c(92576, 
90319, 32101, 35004), .Dim = c(2L, 2L), .Dimnames = structure(list(
    c("Male", "Female"), c("0", "1")), .Names = c("", "")), class = "table"), 
    EthnicGroup = structure(c(140393, 10225, 4708, 20687, 2452, 
    4430, 57125, 2456, 1139, 5055, 518, 812), .Dim = c(6L, 2L
    ), .Dimnames = structure(list(c("White", "Asian", "Black", 
    "Check", "Mixed", "Other"), c("0", "1")), .Names = c("", 
    "")), class = "table"))
Expected <- list(AgeGroup = structure(c(42856.68798, 4482.39066, 12635.84976, 
9647.34546, 38617.18188, 13818.81462, 32353.39392, 28483.33572, 
15724.31202, 1644.60934, 4636.15024, 3539.65454, 14168.81812, 
5070.18538, 11870.60608, 10450.66428), .Dim = c(8L, 2L), .Dimnames = structure(list(
    c("18-34", "0", "1-4", "13-17", "35-54", "5-12", "55-74", 
    "75+"), c("0", "1")), .Names = c("", ""))), Sex = structure(c(91211.19966, 
91683.80034, 33465.80034, 33639.19966), .Dim = c(2L, 2L), .Dimnames = structure(list(
    c("Male", "Female"), c("0", "1")), .Names = c("", ""))), 
    EthnicGroup = structure(c(144500.21844, 9277.16598, 4277.54826, 
    18832.33236, 2172.7926, 3834.94236, 53017.78156, 3403.83402, 
    1569.45174, 6909.66764, 797.2074, 1407.05764), .Dim = c(6L, 
    2L), .Dimnames = structure(list(c("White", "Asian", "Black", 
    "Check", "Mixed", "Other"), c("0", "1")), .Names = c("", 
    ""))))

标签: rlistapplytidyverse

解决方案


library(tidyverse)

# update column names
Obs = map(Observed, ~{as.data.frame.matrix(.x) %>% setNames(paste0("Obs_", names(.)))})

Exp = map(Expected, ~{as.data.frame.matrix(.x) %>% setNames(paste0("Exp_", names(.)))})

# bind columns of updated lists
# round columns
# reorder columns
map2(Obs, Exp, cbind) %>%
  map(~{.x %>% mutate_all(round) %>% select(matches("0"), everything())})

# $`AgeGroup`
#   Obs_0 Obs_1 Exp_0 Exp_1
# 1 49841  8740 42857 15724
# 2  4440  1687  4482  1645
# 3 14537  2735 12636  4636
# 4 11531  1656  9647  3540
# 5 41580 11206 38617 14169
# 6 17091  1798 13819  5070
# 7 27807 16417 32353 11871
# 8 16068 22866 28483 10451
# 
# $Sex
#   Obs_0 Obs_1 Exp_0 Exp_1
# 1 92576 32101 91211 33466
# 2 90319 35004 91684 33639
# 
# $EthnicGroup
#   Obs_0 Obs_1  Exp_0 Exp_1
# 1 140393 57125 144500 53018
# 2  10225  2456   9277  3404
# 3   4708  1139   4278  1569
# 4  20687  5055  18832  6910
# 5   2452   518   2173   797
# 6   4430   812   3835  1407

推荐阅读