{"action":"created","issue":{"url":"https://api.github.com/repos/gergness/srvyr/issues/3","labels_url":"https://api.github.com/repos/gergness/srvyr/issues/3/labels{/name}","comments_url":"https://api.github.com/repos/gergness/srvyr/issues/3/comments","events_url":"https://api.github.com/repos/gergness/srvyr/issues/3/events","html_url":"https://github.com/gergness/srvyr/issues/3","id":113063167,"number":3,"title":"Factors in survey_total","user":{"login":"carlganz","id":11653794,"avatar_url":"https://avatars.githubusercontent.com/u/11653794?v=3","gravatar_id":"","url":"https://api.github.com/users/carlganz","html_url":"https://github.com/carlganz","followers_url":"https://api.github.com/users/carlganz/followers","following_url":"https://api.github.com/users/carlganz/following{/other_user}","gists_url":"https://api.github.com/users/carlganz/gists{/gist_id}","starred_url":"https://api.github.com/users/carlganz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/carlganz/subscriptions","organizations_url":"https://api.github.com/users/carlganz/orgs","repos_url":"https://api.github.com/users/carlganz/repos","events_url":"https://api.github.com/users/carlganz/events{/privacy}","received_events_url":"https://api.github.com/users/carlganz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-10-23T17:22:30Z","updated_at":"2015-10-23T19:10:01Z","closed_at":null,"body":"Hey Greg, \r\nAs I find bugs and come up with ideas for things to do I will raise new issues, but obviously none of the issues are urgent.\r\n\r\nHere is an issue with factors in survey_total that I've found:\r\n\r\nlibrary(srvyr)\r\nlibrary(survey)\r\ndata(api)\r\n#create svy object\r\nsrs_design<-apisrs %>% design_survey(fpc=fpc)\r\nsrs_design\r\n#works correctly\r\nsrs_design %>% summarise(totals=survey_total(enroll))\r\n#works correctly\r\nsrs_design %>% summarise(means=survey_mean(enroll))\r\n#doesn't work correctly\r\nsrs_design %>% summarise(totals=survey_total(stype))\r\n\r\n#how it works with the regular survey package\r\nsrs_design1<-svydesign(id=~1,fpc=~fpc,data=apisrs)\r\nsvytotal(~stype,srs_design1)\r\n\r\nEssentially with factors, svytotal gives the weighted frequency for each level of the factor. Same issue with character variables. I will investigate further this weekend.\r\n\r\nRegards"},"comment":{"url":"https://api.github.com/repos/gergness/srvyr/issues/comments/150666118","html_url":"https://github.com/gergness/srvyr/issues/3#issuecomment-150666118","issue_url":"https://api.github.com/repos/gergness/srvyr/issues/3","id":150666118,"user":{"login":"gergness","id":2104579,"avatar_url":"https://avatars.githubusercontent.com/u/2104579?v=3","gravatar_id":"","url":"https://api.github.com/users/gergness","html_url":"https://github.com/gergness","followers_url":"https://api.github.com/users/gergness/followers","following_url":"https://api.github.com/users/gergness/following{/other_user}","gists_url":"https://api.github.com/users/gergness/gists{/gist_id}","starred_url":"https://api.github.com/users/gergness/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gergness/subscriptions","organizations_url":"https://api.github.com/users/gergness/orgs","repos_url":"https://api.github.com/users/gergness/repos","events_url":"https://api.github.com/users/gergness/events{/privacy}","received_events_url":"https://api.github.com/users/gergness/received_events","type":"User","site_admin":false},"created_at":"2015-10-23T19:10:01Z","updated_at":"2015-10-23T19:10:01Z","body":"Good point, I have only tested on numeric variables so far. Presents a bit of a challenge for the api also, because what if you want to summarize by variables with different number of categories. \r\n\r\nMy best idea so far is to allow for the inclusion of the factor/character variable in `group_by()` and no variable passed into survey_mean / survey_total would indicate that you want to summarize over groups. \r\n\r\n``` r\r\nlibrary(srvyr)\r\nlibrary(survey)\r\ndata(api)\r\n#create svy object\r\nsrs_design<-apisrs %>% design_survey(fpc=fpc)\r\n\r\nout_survey <- svytotal(~stype, srs_design)\r\n\r\n# Doesn't work yet:\r\nout_srvyr <- srs_design %>% \r\n    group_by(stype) %>% \r\n    summarise(total = survey_total(), prop = survey_mean())\r\n\r\n\r\nexpect_equal(out_survey[[\"total\"]], unname(out_survey[1:3]))\r\n\r\n```\r\n\r\nI think this should be just as expressive as survey, if we 'peel' the outermost group.\r\n``` r\r\nsvyby(~stype, ~awards, srs_design, svymean)\r\nsrs_design %>%\r\n\tgroup_by(awards, stype) %>% # Is this the right order? Need to make sure.\r\n\tsummarize(mean = survey_mean())\r\n\r\nsvyby(~awards, ~stype, srs_design, svymean)\r\nsrs_design %>%\r\n\tgroup_by(stype, awards) %>%\r\n\tsummarize(mean = survey_mean())\r\n\t\r\nsvymean(~paste(stype, awards, sep = \"_\"), srs_design)\r\nsrs_design %>%\r\n\tgroup_by(stype_awards = paste(stype, awards, sep = \"_\")) %>%\r\n\tsummarize(mean = survey_mean())\r\n```\r\n\r\n\r\nIf I go this route, need to: \r\n- add informative error if survey_total or survey_mean receive a factor or character, \r\n- get survey_total / survey_mean to understand what to do when no variable is passed in, including 'peeling' off a grouping variable\r\n\r\nThanks!"}}