I am setting up a function to make it easier to run weights:wtd.chi.sq on a number of columns simultaneously.
Here is my sample data:
library(weights)
data_in <- read_table2("Q50_1 Q50_2 Q38 Q90 pov gender wgt1 wgt2
never always Yes 2 High M 1.3 0.4
sometimes always No 2 Med F 0.4 0.2
always sometimes Yes 4 Low F 1.2 0.7
never never No 2 High M 0.5 0.7
always always No 4 High M 0.7 0.8
sometimes never Yes 3 Low F 0.56 0.3
sometimes never Yes 2 Med F 0.9 0.1
")
I would use this line to find the chi sq value for Q50_1 by pov:
wtd.chi.sq(data_in$Q50_1,data_in$pov, weight = data_in$wgt1)
Chisq df p.value
7.3395092 4.0000000 0.1189981
Since I need to repeat this for a df with thousands of variables, I have set up a function to run it. wtd.chi.sq takes vectors, not column names, so it's a little tricky to feed the relevant column in. The function I wrote looks like this:
xtab_func <- function(dat, col, target, wgt){
col <- sym(col)
target <- enquo(target)
wgt <- enquo(wgt)
weights::wtd.chi.sq(noquote(paste0(dat,"$",!!target)), noquote(paste0(dat,"$",!!col)), weight = noquote(paste0(dat,"$",!!wgt)))
}
But when I run it:
xtab_func('dat', 'Q50_1','pov','wgt1')
Error: Quosures can only be unquoted within a quasiquotation context.
# Bad:
list(!!myquosure)
# Good:
dplyr::mutate(data, !!myquosure)
Run `rlang::last_error()` to see where the error occurred.
Could anyone tell me what is going on here?!