【发布时间】:2019-02-24 16:53:41
【问题描述】:
我有两个表列表,分别名为 Observed 和 Expected,其中包含具有相应名称的表和匹配的 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("",
""))))
【问题讨论】:
-
您在寻找
cbind和round吗? -
您的列表并不完全相同。
class = "table"在Expected结构中的每一列之后都缺失。真的是这样吗?