write a function with formula argument and LHS := in tidyeval style

Hello guys!

Assume there is a fake data.

data_frame <- tribble(
  ~case, ~valueA, ~valueB,
  "case1", 1.0, 2.0,
  "case1", 1.2, 2.5,
  "case1", 1.5, 2.9,
  "case2", 2.0, 5.0,
  "case2", 2.2, 5.4,
  "case2", 2.3, 5.7,
  "case3", 3.0, 7.1,
  "case3", 3.3, 7.7,
  "case3", 4.0, 7.9
)

what I want to implement is to applied wilcox.test to different value in different case with tidyeval style.

if the fuction is function(data,group,value)
In this dataset it should be :

  • when type function(data_frame,case,valueA)
    it should give me A wilcox.test result that valueA between case1-case2,case1-case3,case2-case3
  • when type function(data_frame,case,valueB)
    it should give me A wilcox.test result that valueB between case1-case2,case1-case3,case2-case3

Below is what I tried but it seemed that the filter section has some problem and I am not sure if the formula part is right. :cry:
I am using dplyr 0.8.5 so the {{}} syntax seems not support right now.

library(tidyverse)
mutiple_wilcox <- function(data,group,value,...){
  group_e <- enquo(group)
  lhs_e <- enexpr(value)
  rhs_e <- enexpr(group)
  group_combn <- combn(unique(data[[as_label(group_e)]]),2)
  group_combn <- as_tibble(t(group_combn))
  df <- map2_dfr(group_combn[[1]],group_combn[[2]],
                 ~.data %>% filter(!!group_e:= .x|!!group_e:=.y) 
                 %>% eval_tidy(quo(wilcox.test(!!lhs_e~!!rhs_e,data=.,...))) 
                 %>% broom::glance())
  wilcox_result <- bind_cols(group_combn,df)
  wilcox_result
}

Could someone give some advice? :face_with_raised_eyebrow:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.