R: svyby print NA as 0

Viewed 68

I have a problem with svyby function, because in the result I want to print all values, but svyby automatically delete all NAs.

result <- svyby(formula = pop, by=~col1+col2, FUN=svytotal, design = mydesign, na.rm.all = TRUE)

How to print all the values even with 0?

UPDATE:

I found a solution by adding parameter drop.empty.groups = FALSE

2 Answers

Followup: svyby does not delete NAs but it does (by default) delete groups that have no observations. You can use drop.empty.groups=FALSE to report the value (which will probably be NA) even for groups with no observations.

just warning this isn't great statistical practice but i assume you have a formatting reason to do this :-)

library(survey)
data(api)
x <- apiclus1
x[1,'api99'] <- NA
dclus1<-svydesign(id=~dnum, weights=~pw, data=x, fpc=~fpc)
result <- svyby(~api99, ~stype, dclus1, svymean)
result
result[ is.na( result ) ] <- 0
result
Related