I am using dplyr and trying to create a function to calculate p.values based on grouping arguments. I would like to be able to have an argument that would be list of any len
As mentioned by @lionel - one of the lead developers of dplyr
in this comment
You want the quoting to be external and explicitly done by the user rather than implicitly by your function. To this end you can ask your users to quote with base::alist(), rlang::exprs(), or dplyr::vars()
You can do something like this for your question
grouped.t.test2 <- function(dataset, subset.plot, comparison, group_vars) {
if (is.null(subset.plot)) {
subset.plot <- dataset[['variable']]
}
filter(dataset, variable %in% subset.plot) %>%
group_by(!!! group_vars) %>%
do(tidy(t.test(x = .$value[.[comparison] == levels(.[[comparison]])[1]],
y = .$value[.[comparison] == levels(.[[comparison]])[2]]))) %>%
mutate(p.value.format = symnum(p.value, corr = FALSE, na = FALSE,
cutpoints = c(0, 0.0001, 0.001, 0.01, 0.05, 1),
symbols = c("****", "***", "**", "*", NA))) %>%
arrange(!!! group_vars)
}
grouped.t.test2(dataset = dataset, subset.plot = NULL, comparison = 'Genotype',
alist(variable, Day))
# or
grouped.t.test2(dataset = dataset, subset.plot = NULL, comparison = 'Genotype',
dplyr::vars(variable, Day))
# A tibble: 8 x 13
# Groups: variable, Day [8]
variable Day estimate estimate1 estimate2 statistic p.value parameter
<fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 % CD127+ 8 -3.24 1.66 4.90 -4.26 9.93e-4 12.6
2 % CD127+ 15 -24.4 31.1 55.5 -3.80 2.88e-3 11.2
3 % CD127+ 22 -22.1 27.4 49.5 -4.60 5.54e-4 12.5
4 % CD127+ 30+ -28.6 36.8 65.4 -5.23 1.36e-4 13.7
5 % KLRG1+ 8 23.8 81.2 57.4 9.79 3.11e-7 12.5
6 % KLRG1+ 15 16.5 73.7 57.2 3.78 2.08e-3 13.8
7 % KLRG1+ 22 20.9 70.1 49.2 4.44 4.82e-4 14.9
8 % KLRG1+ 30+ 22.5 76.7 54.2 4.46 6.01e-4 13.4
# ... with 5 more variables: conf.low <dbl>, conf.high <dbl>,
# method <fct>, alternative <fct>, p.value.format <chr>