组合多个不同行的对象

问题描述 投票:0回答:1

我有以下对象:

> freq
    0     1       
11027  1264 12291 

> wf
                  total     SE
df[[var_name]]0 4327145  98763
df[[var_name]]1  691647  48336
Total           5018792 108969

> prop
                     mean        SE   DEff
df[[var_name]]0 0.8621886 0.0087937 8.0181
df[[var_name]]1 0.1378114 0.0087937 8.0181

> CI
                    2.5 %    97.5 %
df[[var_name]]0 0.8449534 0.8794239
df[[var_name]]1 0.1205761 0.1550466

> CV
df[[var_name]]0 df[[var_name]]1 
     0.01019923      0.06380940 

我需要将它们全部合并到一个表中,其中包含每个对象的所有列。对象 freqwf 有 3 行,而其他对象有 2 行,因此在我的最终结果中,我想要一个包含 3 行的表,其中对于每个包含 2 行的对象,最后一行将为空白。我的最终结果应该是这样的:

这是对象的类型

> dput(freq)
c(`0` = 11027L, `1` = 1264L, 12291L)
> dput(wf)
structure(c(`df[[var_name]]0` = 4327145.41087343, `df[[var_name]]1` = 691646.58912879, 
Total = 5018792.00000222), class = "svystat", var = structure(c(9754076450.14318, 
-108168197.221403, 9645908252.92177, -108168197.221403, 2336411279.25803, 
2228243082.03663, 9645908252.92177, 2228243082.03663, 11874151334.9584
), .Dim = c(3L, 3L), .Dimnames = list(c("df[[var_name]]0", "df[[var_name]]1", 
"Total"), c("df[[var_name]]0", "df[[var_name]]1", "Total"))), statistic = "total")
> dput(prop)
structure(c(`df[[var_name]]0` = 0.862188632418223, `df[[var_name]]1` = 0.137811367581777
), var = structure(c(7.73284606519557e-05, -7.73284606519557e-05, 
-7.73284606519557e-05, 7.73284606519557e-05), .Dim = c(2L, 2L
), .Dimnames = list(c("df[[var_name]]0", "df[[var_name]]1"), 
    c("df[[var_name]]0", "df[[var_name]]1"))), statistic = "mean", class = "svystat", deff = structure(c(8.01805078282499, 
8.01805078282499, 8.01805078282499, 8.01805078282499), var = structure(c(4.05825805051452e-05, 
-4.05825805051452e-05, -4.05825805051452e-05, 4.05825805051452e-05, 
-4.05825805051452e-05, 4.05825805051452e-05, 4.05825805051452e-05, 
-4.05825805051453e-05, -4.05825805051452e-05, 4.05825805051452e-05, 
4.05825805051452e-05, -4.05825805051453e-05, 4.05825805051452e-05, 
-4.05825805051453e-05, -4.05825805051453e-05, 4.05825805051453e-05
), .Dim = c(4L, 4L), .Dimnames = list(c("df[[var_name]]0", "df[[var_name]]0", 
"df[[var_name]]1", "df[[var_name]]1"), c("df[[var_name]]0", "df[[var_name]]0", 
"df[[var_name]]1", "df[[var_name]]1"))), statistic = "variance", class = c("svyvar", 
"svystat"), .Dim = c(2L, 2L), .Dimnames = list(c("df[[var_name]]0", 
"df[[var_name]]1"), c("df[[var_name]]0", "df[[var_name]]1"))))
> dput(CI)
structure(c(0.844953375029307, 0.120576110192862, 0.879423889807138, 
0.155046624970693), .Dim = c(2L, 2L), .Dimnames = list(c("df[[var_name]]0", 
"df[[var_name]]1"), c("2.5 %", "97.5 %")))
> dput(CV)
c(`df[[var_name]]0` = 0.010199230102026, `df[[var_name]]1` = 0.0638093969147096
)


r multiple-columns bind
1个回答
0
投票

好的。完全没问题。根据您提供的数据,我制作了一个可以实现这一目的的单行代码。

data.table(var_name=c("0","1","Total"), freq=t(freq), total=wf$total, se=wf$SE, mean=prop$mean, "2.50%"=CI$`2.5 %`, "97.50%"=CI$`97.5 %`, CV=t(CV))

注意,如果您想在列名称中使用“%”,请使用

data.table
来包含您的数据。
Data.frame
默认情况下不允许使用特殊字符。

编辑: 好吧,既然你的数据是

atomic vectors
,我就改变访问列的方式。

data.table(var_name=c("0","1","Total"), freq=freq, total=wf[['total']], se=wf[['SE']], mean=prop[['mean']], "2.50%"=CI[['2.5 %']], "97.50%"=CI[['97.5 %']], CV=CV)
© www.soinside.com 2019 - 2024. All rights reserved.