Sergio
Sergio

Reputation: 11

Problem filtering cases and using svytable with R

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.

Upvotes: 1

Views: 328

Answers (1)

RafSR
RafSR

Reputation: 51

It worked to me =

survey::svytable((~ variable1+ variables3),ObjectSurvey[DataFrame$Variable3==ValueX])

Adding "[DataFrame$Variable3==ValueX]" will filter the date using the data frame.

Upvotes: 0

Related Questions