'R: svyby print NA as 0
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
Solution 1:[1]
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.
Solution 2:[2]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Thomas Lumley |
| Solution 2 | Anthony Damico |
