I'm having problems with svytable not recognizing or ignoring some recoding I've done. This is for a shiny app, and I have the same code with the same data in another app and am having no problems with it, but any other version I try, even with the exact same dataset with a different name, it doesn't work. It seems to be ignoring the names and the responses that I combined. Am I missing something? Thanks for any help you might be able to provide!
SexualOrientation<-c(1, 2, 1, 1, 1, 3, 4, 2, 1, 2, 2, 3, 4, 2, 2, 3, 3, 4, 4, 1, 1, 2)
q121<-c(1, 1, 1, 2, 3, 4, 1, 1, 1, 1, 2, 2, 2, 1, 2, 1, 1, 2, 1, 1, 1, 1)
q122<-c(2, 1, 1, 2, 1, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2)
weight<-c(10.9384, 16.68, 16.68, 26.6481, 11.7255, 10.9384, 10.9384, 10.9384, 16.68, 11.7255, 8.5349, 10.9384, 16.68, 16.68, 16.68, 16.68, 10.9384, 14.2632, 11.7255, 12.57901, 19.182, 9.8151)
stratum<-c(23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23)
psu<-c(3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1)
d<-data.frame(SexualOrientation,q121,q122,weight,stratum,psu)
d$homeless <- ifelse(d$q121>1, 1, 0)
d$unaccompanied <- ifelse(d$q122==1, 1, 0)
d$homelessgroups[d$homeless==1 & d$unaccompanied==0] <- "Homeless with Family"
d$homelessgroups[d$homeless==0 & d$unaccompanied==1] <- "Homeless, Unaccompanied"
d$homelessgroups[d$homeless==1 & d$unaccompanied==1] <- "Both Homeless"
d$homelessgroups[d$homeless==0 & d$unaccompanied==0] <- "Housed"
d$homelessgroups <- ordered(d$homelessgroups, levels = c("Housed", "Homeless with Family", "Homeless, Unaccompanied", "Both Homeless"))
d$allhomeless <- ifelse(d$homelessgroups=="Housed", "Housed", "Homeless")
d_design <- svydesign(id=~psu, weight=~weight, strata=~stratum, nest = T, data=d)
d$SexualOrientation[d$SexualOrientation==1] <- "Heterosexual"
d$SexualOrientation[d$SexualOrientation==2 | d$SexualOrientation==3] <- "Gay or Lesbian or Bisexual"
d$SexualOrientation[d$SexualOrientation==4] <- "Not Sure"
d$SexualOrientation <- ordered(d$SexualOrientation, levels = c("Heterosexual", "Gay or Lesbian or Bisexual", "Not Sure"))
soweight <- as.data.frame(svytable(~SexualOrientation + allhomeless, design = d_design))
soweight <- rename(soweight, Var1 = SexualOrientation, Var2 = allhomeless, value = Freq)
soweight$value <- round(soweight$value, 0)
This is the output I'm getting (with the sample data above)
And this is the output I want/achieved with the same code elsewhere (and the real data).