-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for data weights #81
Comments
I am very new to R, so I not in a position to make a PR for that yet, but in essence using weights would mean that instead of using counts of rows, sums of weight variable values of respective data rows should be used to get the "weighted" results. |
Hi Tonis, As of the current state, I'm not sure how to support weights in crosstable. All variables are described using the You can work this around by using a fixed reference to the weights, but it won't work with library(tidyverse)
library(crosstable)
a = mtcars2 %>%
mutate(w = ifelse(am=="manual", 1, 2))
#works
a %>%
crosstable(where(is.numeric),
funs=c(mean=~mean(.x), weighted.mean = ~weighted.mean(.x, w=a$w)))
#doesn't work
a %>%
crosstable(where(is.numeric), by=vs,
funs=c(mean=~mean(.x), weighted.mean = ~weighted.mean(.x, w=a$w))) If you (or anyone) have an idea of how I could implement this, I'd gladly add the feature. EDITI understood your question as it was about numerical variables. For categorical variables, you could use a %>%
crosstable(am)
a %>%
uncount(w) %>%
crosstable(am) Obviously, that will only work if your weighting variable is integer. If you know of a package that does that, please let me know. |
Thanks for looking into it! I am testing multiple crosstab packages to see the one more suitable, here is one that has weights implemented: |
On GitHub, it's always a good idea to accompany a statement with code 😉 |
I have not tried the expss with weights yet, its just what they state on their package page and what I noticed here, I have yet no Idea if this actually works, sry :) |
Describe the new feature
An option to use weight column for any calculations to use data weights.
I do not find the option in source code, but I found an existing question here:
https://stackoverflow.com/questions/71224873/how-to-use-weight-with-the-package-crsosstable-for-r
The text was updated successfully, but these errors were encountered: