juba / questionr

R package to make surveys processing easier
https://juba.github.io/questionr/
82 stars 17 forks source link

adding Xtabs() #96

Closed rdrr1990 closed 6 years ago

rdrr1990 commented 6 years ago

Adds Xtabs(), which is a wrapper function for wtd.table. Xtabs returns a table formatted in a style popular in the polling world. The first column is the overall result. Additional column(s) are then given for each factor level for one or more y variables.

kable(Xtabs(hdv2003, x = "trav.satisf", y = c("sexe", "trav.imp"), weight = "poids"))
|               |Overall |Homme |Femme |Le plus important |Aussi important que le reste |Moins important que le reste |Peu important |
|:--------------|:-------|:-----|:-----|:-----------------|:----------------------------|:----------------------------|:-------------|
|Satisfaction   |46.6%   |24.3% |22.4% |0.873%            |12.4%                        |32%                          |1.36%         |
|Insatisfaction |11.1%   |6.44% |4.69% |0.29%             |2.66%                        |5.98%                        |2.2%          |
|Equilibre      |42.2%   |22.5% |19.7% |0.785%            |9.65%                        |30.6%                        |1.19%         |

By default, returns a percentage but the user may also request proportion or counts. Overall, I've done my best to keep the style similar to wtd.table except that it seemed best to have the user pass a data.frame. (And I called it Xtabs to avoid confusion with stats::xtabs which, though similar in spirit, does not return the desired format).

roxygen2 documentation provided. Passes R CMD check --as-cran without error, warning, or note (other than maintainer name).

juba commented 6 years ago

Hi,

First, sorry for the delay of my answer, and thanks for your pull request.

Your function could be a useful addition, but there is one small problem for me : the result is a table with character values. I would have prefer Xtab() to work the same way as rprop and cprop for example : return a table of numbers of class proptab and use the format.proptab and print.proptab functions to handle printing in console. Note that there's no need to affect a proptab class when the results are counts.

I hope I'm clear enough. Do you think you could take a look into this ?

Thanks !

rdrr1990 commented 6 years ago

Good to hear from you! That sounds very doable. I will look into those printing / formatting methods and be in touch.

juba commented 6 years ago

Thanks ! And sorry not to have answered sooner.

rdrr1990 commented 6 years ago

No problem!

On Tue, Nov 28, 2017 at 1:02 PM, Julien Barnier notifications@github.com wrote:

Thanks ! And sorry not to have answered sooner.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/juba/questionr/pull/96#issuecomment-347662405, or mute the thread https://github.com/notifications/unsubscribe-auth/ASWlXO0KHwloY0ZbzFNCfkcQtvj5r_eZks5s7HTkgaJpZM4QSrHB .

-- Pete Mohanty, PhD Science, Engineering & Education Fellow Stanford University Department of Statistics https://sites.google.com/site/petemohanty/

juba commented 6 years ago

Hi,

I just merged a slightly changed version of your pull request.

The two main changes are :

Otherwise it is mainly a bit of code cleaning.

I'll let you take a look at it and tell me if it is ok to you.

Anyway, thanks a lot for your suggestion and your pull request !

rdrr1990 commented 6 years ago

Glad to hear it! Those changes make sense too. Cheers