1

I'm new to R (coming recently from SPSS) and I'm having a really hard time trying to find a solution for this problem.

Dataset I'm working with has almost 300 variables from more than 3000 people, and it has protected info about health I can't show :/

This is a very small part of it (I'm not sure if it really helps at all):

structure(list(SEXO = structure(c(1L, 2L, 1L, 2L, 2L, 1L, 1L, 
1L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("Mujer", 
"Hombre"), class = "factor"), UNIC_6m = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = c("Sí", "No"), class = "factor"), UN_sumaAC_R = c(0, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), UN_sumaDC_R = c(1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1)), row.names = c(NA, 
20L), class = "data.frame")

Using svytable I can get weighted frequencies for the variable UN_sumaAC_R

round(addmargins(svytable(~UN_sumaAC_R, design = f_design)),0)

UN_sumaAC_R
  0   1  10 100 Sum 
 14 264   2   5 284 

I can also get proportions...

round(100*prop.table(svytable(~UN_sumaAC_R, design = f_design)),2)

UN_sumaAC_R
    0     1    10   100 
 4.77 92.88  0.75  1.59 

The problem comes when I try to get proportions only for UN_sumaAC_R levels 1, 10 and 100 (and dropping 0). I can do it with unweighted data with this code:

100*prop.table(table(
  f %>%
  select(UN_sumaAC_R) %>%
  subset(f$UN_sumaAC_R >0)
))

        1        10       100 
97.250859  1.030928  1.718213

But I couldn't do it with any combination of "filter", "subset" and "svytable"... I'm pretty sure the solution is very simple, but I can't find it.

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Sergio
  • 11
  • 2
  • Welcome to SO. To help us to help would you mind sharing [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data. To share your data, you could type `dput(NAME_OF_DATASET)` into the console and copy & paste the output starting with `structure(....` into your post. If your dataset has a lot of observations you could do e.g. `dput(head(NAME_OF_DATASET, 20))` for the first twenty rows of data. – stefan Nov 28 '21 at 08:47
  • are you looking to subset your survey design with `subsetted_design <- subset( f_design , UN_sumaAC_R > 0 )` ? and then `svytable()` using the `subsetted_design` instead of `f_design` ? – Anthony Damico Dec 12 '21 at 12:40

0 Answers0