Search code examples
rsurveycrosstable

Is there a way to paste 2 "ftable" object?


I`m using svyby from survey to make some weighted ratios with their standard deviation and coefficient of variation, I use the code below to generate a three way table which provide me the ratio of subocup compared to ocup for male teleworker Hombre_subocup/ocup, female teleworkers Mujer_subocup/ocup and the exact same for non teleworkers males and females.

tasasub_tele_sex319<-ftable(svyby(~subocup, by= ~ tele_sinco11 + SEX, subset(sd319, EDA >= 15 & POS_OCU != "Trabajadores sin pago"), denominator = ~ocup, deff= T, level = 0.9, vartype = c("cv", "se"), na.rm.by =  T,svyratio))

tasasub_tele_sex319

And get:

                        SEX_
tele_sinco11_             Hombre_subocup/ocup Mujer_subocup/ocup
  No teletrabaja_svyratio         0.084149460        0.080230723
  No teletrabaja_cv               0.001782653        0.002152965
  No teletrabaja_SE               0.021184369        0.026834665
  No teletrabaja_DEff             3.896438704        3.593164119
  Teletrabaja_svyratio            0.036515518        0.027384514
  Teletrabaja_cv                  0.003075113        0.002250647
  Teletrabaja_SE                  0.084213867        0.082186856
  Teletrabaja_DEff                2.325352651        2.039503624

But I also need the total value of the ratio (without SEX), so I use this code and get this output:

tasasub_tele_tot319<-ftable(svyby(~subocup, by= ~ tele_sinco11, subset(sd319, EDA >= 15 & POS_OCU != "Trabajadores sin pago"), denominator = ~ocup, deff= T, level = 0.9, vartype = c("cv", "se"), na.rm.by =  T,svyratio))

tasasub_tele_tot319

>tasasub_tele_tot319

                        subocup/ocup
tele_sinco11                         
No teletrabaja svyratio   0.082730583
               cv         0.001537011
               SE         0.018578512
               DEff       4.722529515
Teletrabaja    svyratio   0.031475714
               cv         0.001902234
               SE         0.060434971
               DEff       2.299934447

This would be the value of the ratio without any strata (a simple two way table).

What I want to do is to paste the last output to the former, so I get a male ratio column, female ratio column and total ratio column in the same table, so more or less it should look like this:

enter image description here

I wouldn't mind if the "total" columns goes first.


Solution

  • The solution is to combine first, then ftable. Let's do this with a built-in data set:

    > a<-svyby(~api.stu,denom=~enroll, ~stype+sch.wide, dclus1,svyratio)
    > b<-svyby(~api.stu,denom=~enroll, ~stype, dclus1,svyratio)
    > a
          stype sch.wide api.stu/enroll se.api.stu/enroll
    E.No      E       No      0.8794583        0.01429043
    H.No      H       No      0.8386232        0.01170588
    M.No      M       No      0.8390440        0.01478048
    E.Yes     E      Yes      0.8508618        0.01231925
    H.Yes     H      Yes      0.8286597        0.01622480
    M.Yes     M      Yes      0.8589915        0.01272169
    > b
      stype api.stu/enroll se.api.stu/enroll
    E     E      0.8532672        0.01253361
    H     H      0.8300683        0.01472607
    M     M      0.8536738        0.01114203
    
    

    These have different column names, but we can fix that

    >  b$sch.wide<-"Total"
    > b
      stype api.stu/enroll se.api.stu/enroll sch.wide
    E     E      0.8532672        0.01253361    Total
    H     H      0.8300683        0.01472607    Total
    M     M      0.8536738        0.01114203    Total
    
    

    And now combine them

    > ftable(rbind(a,b))
                   sch.wide             No            Yes          Total
                            api.stu/enroll api.stu/enroll api.stu/enroll
    stype                                                               
    E     svyratio              0.87945833     0.85086183     0.85326723
          SE                    0.01429043     0.01231925     0.01253361
    H     svyratio              0.83862316     0.82865975     0.83006825
          SE                    0.01170588     0.01622480     0.01472607
    M     svyratio              0.83904396     0.85899149     0.85367375
          SE                    0.01478048     0.01272169     0.01114203