组合(绑定列)两个具有不同类的对象

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

我想合并 R 中每个级别具有相同名称的 2 个对象的结果。我有 wf1 对象和

CI1
对象(如下),但是当我使用
cbind
命令组合它们时,我没有从 wf1 对象中获取所有列。
cbind
的结果位于代码末尾。它将对象名称替换为列名称,并从
wf1
中删除“SE”列之一。

wf1<-svytotal(~factor(var1)+Total, mydesign)
wf1
                 total      SE
  factor(var1)0  62469  7620.4
  factor(var1)1 151486  9421.3
  Total           213955 13418.5

CI1<-confint(wf1)
CI1
                    2.5 %    97.5 %
  factor(var1)0  47532.84  77404.17
  factor(var1)1 133020.75 169951.38
  Total           187654.80 240254.33

cbind(wf1, CI1)
                     wf1     2.5 %    97.5 %
  factor(var1)0  62468.5  47532.84  77404.17
  factor(var1)1 151486.1 133020.75 169951.38
  Total           213954.6 187654.80 240254.33

我希望最终的结果是这样的:

                    total       SE      2.5 %     97.5 %
  factor(var1)0      2469  7620.4   47532.84  77404.17
  factor(var1)1    151486  9421.3   133020.75 169951.38
  Total            213955  13418.5  187654.80 240254.33

这里出了什么问题?

这是

dput(wf1)
dput(CI1)
的结果:

wf1 <- structure(c(`factor(Dummy1)0` = 62468.5031180821, 
                   `factor(Dummy1)1` = 151486.062913796, 
                   Total = 213954.566031878), 
                 var = structure(c(58070142.8887675, 16612955.6715334, 
                                   74683098.560301, 16612955.6715334, 
                                   88759974.1026789, 105372929.774212, 
                                   74683098.560301, 105372929.774212, 
                                   180056028.334513 ), 
                                 .Dim = c(3L, 3L), 
                                 means = c(62468.5031180821, 
                                           151486.062913796, 
                                           213954.566031878)), 
                 statistic = "total", class = "svrepstat")


CI1 <- structure(c(47532.8390009755, 133020.749879858, 187654.797624822, 
                   77404.1672351886, 169951.375947734, 240254.334438934), 
                 .Dim = 3:2, 
                 .Dimnames = list( c("factor(Dummy1)0", 
                                     "factor(Dummy1)1", 
                                     "Total"), 
                                   c("2.5 %", "97.5 %")))
r object matrix types cbind
1个回答
3
投票

您需要将

wf1
"svrepstat"
转换为
"data.frame"
:

library(survey)

class(wf1)
#> [1] "svrepstat"
class(CI1)
#> [1] "matrix" "array"

cbind(as.data.frame(wf1), CI1)
#>                    total        SE     2.5 %    97.5 %
#> factor(Dummy1)0  62468.5  7620.377  47532.84  77404.17
#> factor(Dummy1)1 151486.1  9421.251 133020.75 169951.38
#> Total           213954.6 13418.496 187654.80 240254.33

如果您想重命名

rownames
,您可以通过将
cbind
的输出分配给变量并更改它们来实现;
colnames
也是如此:

out <- cbind(as.data.frame(wf1), CI1)

rownames(out) <- c("0", "1", "Total")
colnames(out) <- c("Total", "S.E.", "2.5%", "97.5%")

out
#>          Total      S.E.      2.5%     97.5%
#> 0      62468.5  7620.377  47532.84  77404.17
#> 1     151486.1  9421.251 133020.75 169951.38
#> Total 213954.6 13418.496 187654.80 240254.33

数据:


wf1 <- structure(c(`factor(Dummy1)0` = 62468.5031180821, 
                   `factor(Dummy1)1` = 151486.062913796, 
                   Total = 213954.566031878), 
                 var = structure(c(58070142.8887675, 16612955.6715334, 
                                   74683098.560301, 16612955.6715334, 
                                   88759974.1026789, 105372929.774212, 
                                   74683098.560301, 105372929.774212, 
                                   180056028.334513 ), 
                                 .Dim = c(3L, 3L), 
                                 means = c(62468.5031180821, 
                                           151486.062913796, 
                                           213954.566031878)), 
                 statistic = "total", class = "svrepstat")


CI1 <- structure(c(47532.8390009755, 133020.749879858, 187654.797624822, 
                   77404.1672351886, 169951.375947734, 240254.334438934), 
                 .Dim = 3:2, 
                 .Dimnames = list( c("factor(Dummy1)0", 
                                     "factor(Dummy1)1", 
                                     "Total"), 
                                   c("2.5 %", "97.5 %")))

创建于 2023-11-20,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.