datadigger
datadigger

Reputation: 181

Combine (binding columns) two objects with different classes

I want to combine the results of 2 objects in R that have same name for each level. I have wf1 object and CI1 object (below) but when I use the cbind command to combine them I don't get all the columns from wf1 object. The results of cbind is at the end of the code. It replaces object name as the column name and removes one of the columns which is "SE" from wf1.

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

I want the end result to be like this:

                    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

What went wrong here?

This is the results of dput(wf1) and 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 %")))

Upvotes: 2

Views: 100

Answers (1)

M--
M--

Reputation: 29203

You need to convert wf1 from "svrepstat" to a "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

If you want to rename the rownames, you can do so by assigning the output of cbind to a variable and changing them for that; same goes for the 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

Data:


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 %")))

Created on 2023-11-20 with reprex v2.0.2

Upvotes: 3

Related Questions