0
votes

my question has a similar one [here][1], but as long dplyr changes the way it does the things, I believe my question is original and could help other people.

First, I'm trying to Inside a function, reports descriptive statistics and run some statistical inference tests, such as chi-square.

This syntax works very well:

> desc_chi <- function(group_var) {   group_var <- enquo(group_var)   j
> <- dataset %>% 
>     group_by(!!group_var) %>% 
>     summarise(n = n())   print(j) } 
desc_chi(GENDER)

But when I add a Chi-square test, it stops and reports an error:

desc_chi <- function(group_var) {
  group_var <- enquo(group_var) 
  j <- dataset %>% 
    group_by(!!group_var) %>% 
    summarise(n = n(),
      pvalue=chisq.test(.$LOCATION, .$group_var))
  print(j)
}

Column pvalue must be length 1 (a summary value), not 9

Thanks for all support you always provide.

1

1 Answers

1
votes

How about this?

 desc_chi <- function(dataset, group_var) {   
  group_var <- enquo(group_var)   
  dataset %>% 
      group_by(!!group_var) %>% 
      summarise(n = n()) %>% 
      mutate(chisq_pval = chisq.test(n)$p.value)   
 } 


mtcars %>% 
     desc_chi(cyl)


# A tibble: 3 x 3
    cyl     n chisq_pval
   <dbl> <int>      <dbl>
1  4.00    11      0.315
2  6.00     7      0.315
3  8.00    14      0.315