4

I need to create one way frequency table. The tables have to include the proportions for variables.

Using prop.table(svytable()), I can extract proportions for one variable at a time.

library(survey)
round(prop.table(svytable(~varA,design=designA))*100,digits=2)

However, I need codes that join each output for each variable into one single table. I tried the below but it is not a table.

c(output1,output2...)

For example, the expected output for VarA and VarB with 2 levels

|Var Name|Proportion|
|       A|          |
|Yes     |      50.0|
|No      |      50.0|
|       B|          |
|Yes     |      20.0|
|No      |      80.0|

The actual output using c() is just a vector.

Yes No Yes No
50  50 20  80

Sample dput(data)

structure(list(psu = structure(c(1, 1, 2, 2), format.sas = "BEST"), 
strata = structure(c(1, 2, 3, 4), format.sas = "BEST"), wt =structure(c(1.7, 
0.8, 1.2, 0.3), format.sas = "BEST"), residence = structure(c("Urban", 
"Rural", "Urban", "Urban"), format.sas = "$CHAR"), income = structure(c("High", "Low", "Low", "High"), format.sas = "$CHAR")), 
label = "TEST", row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))

Example on how to extract proportion for single variable.

assign library

library(survey)

declare svydesign

testdesign<- svydesign(id=test$psu, strata=test$strata, weight=test$wt, nest=TRUE, data=test)

produce svytable

a=round(prop.table(svytable(~residence,design=testdesign))*100,digits=2)
Community
  • 1
  • 1
newRuser
  • 95
  • 1
  • 7
  • Can you add data in your post using `dput` ? – Ronak Shah Oct 09 '19 at 02:32
  • @RonakShah sorry I am new to R, you are referring dput for data declared using svydesign or the raw data? – newRuser Oct 09 '19 at 03:06
  • `dput` of data from svydesign. `temp <- svytable(~varA,design=designA)` and then copy output of `dput(temp)`. – Ronak Shah Oct 09 '19 at 03:11
  • the output as: structure(c(Urban = 33.17, Rural = 66.83), class = c("svytable", "xtabs", "table"), call = svytable.survey.design(formula = ~residence, design = tldesign), .Dim = 2L, .Dimnames = list(residence = c("Urban", "Rural"))) – newRuser Oct 09 '19 at 03:14
  • Which package is `svytable` from? I am not able to get the data from the above `dput` can you post `dput` fo raw data by editing your post instead of comment? – Ronak Shah Oct 09 '19 at 03:21
  • the dput(original data) is too long, i dput() a sample data instead. Hope the information is useful. svytable is from library(survey) – newRuser Oct 09 '19 at 03:44
  • How do I apply `svytable` to `data` which you have shared ? – Ronak Shah Oct 09 '19 at 04:51
  • refer the code added above :) – newRuser Oct 09 '19 at 05:11

1 Answers1

3

Is this what you are trying to achieve ?

library(survey)
prop.table(svytable(~residence + income,design=testdesign), 1) * 100

#         income
#residence  High   Low
#    Rural   0.0 100.0
#    Urban  62.5  37.5

If we want to combine two outputs we need same column names which can be done via setNames

output1 <- round(prop.table(svytable(~residence,design=testdesign))*100,digits=2)
output2 <- round(prop.table(svytable(~income,design=testdesign))*100,digits=2)

rbind(setNames(as.data.frame(output1), c("Var1", "Freq")), 
      setNames(as.data.frame(output2), c("Var1", "Freq")))

#   Var1 Freq
#1 Rural   20
#2 Urban   80
#3  High   50
#4   Low   50
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I don't require a crosstabulation shown in 1st output. I need to append the output1 and output2 results as a table vertically. The expected output is shown in the question. – newRuser Oct 09 '19 at 07:15
  • 1
    @newRuser The output which you have shown is not a valid R structure. If you want to vertically bind output you need same column names. You can try `rbind(setNames(as.data.frame(output1), c("Var1", "Freq")), setNames(as.data.frame(output2), c("Var1", "Freq"))) ` – Ronak Shah Oct 09 '19 at 07:18
  • that is what i needed – newRuser Oct 09 '19 at 07:32
  • How do you get the confidence intervals for these proportions? – jonestats Feb 23 '23 at 07:03