datadigger
datadigger

Reputation: 181

Combine multiple objects with different rows

I have the following objects:

> 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 

I need to combine all of them into one table that contains all the columns of every object. Object freq and wf have 3 rows while others have 2 rows so in my final results I want a table with 3 rows where the last row would be blank for every object with 2 rows. My final results should look something like this:

enter image description here

here is the type of the object

library(survey) ## load this library for reproducibility 

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

Upvotes: 1

Views: 81

Answers (1)

M--
M--

Reputation: 29203

Same as your previous question: Combine (binding columns) two objects with different classes,

You need to convert to data.frame. Here, You also need to make them the same size, hence, rbind.

library(survey)

cbind(freq, 
      as.data.frame(wf), 
      rbind(as.data.frame(prop), NA),
      rbind(as.data.frame(CI), NA),
      rbind(as.data.frame(CV), NA))
#>    freq     total        SE      mean         SE     deff     2.5 %    97.5 %
#> 0 11027 4327145.4  98762.73 0.8621886 0.00879366 8.018051 0.8449534 0.8794239
#> 1  1264  691646.6  48336.44 0.1378114 0.00879366 8.018051 0.1205761 0.1550466
#>   12291 5018792.0 108968.58        NA         NA       NA        NA        NA
#>           CV
#> 0 0.01019923
#> 1 0.06380940
#>           NA

Upvotes: 1

Related Questions