简体   繁体   中英

Run chi-square test in all columns for a data_frame using dplyr

There are several similar questions that grab chi-square results, but that solves my problem. I'd like to calculate p.values from chi-square tests for all columns in a data_frame and store them in a column within the original data_frame . There will be duplicate values which I'm fine with. Ultimately, I'd like to select all columns in a data_frame that have a p.value lower than x with my variable of choice.

require(dplyr)

my_df <- data_frame(
  one_f = sample(LETTERS[1:5],100,T),
  two_f = sample(LETTERS[4:5],100,T),
  three_f = sample(LETTERS[5],100,T)
)
my_df %>% 
  head()

my_df %>% 
  summarise_all(funs(chisq.test(.,my_df$two_f)$p.value))

Gets me this error:

Error in summarise_impl(.data, dots) : 
  Evaluation error: 'x' and 'y' must have at least 2 levels.


my_df %>% 
  mutate_if(n_distinct>1,fun(chisq.test(.,my_df$two_f)$p.value))

Get me this error:

Error in n_distinct > 1 : 
  comparison (6) is possible only for atomic and list types

I'm looking for something like this.

my_df %>% 
      mutate(p.value = sample(c(0.043,0.87,0.00),nrow(.),T)) %>% 
      head()

Then I plan to use gather and filter then spread to get the significantly associated variables according to my chi-square test.

I suppose

my_df %>% filter(foo,bar >= 0.05)#function that finds p.values and filters by 
# alpha level

would be my ultimate goal.

require(dplyr)
require(tidyr)

my_df <- data_frame(
  one_f = sample(LETTERS[1:5],100,T),
  two_f = sample(LETTERS[4:5],100,T),
  three_f = sample(LETTERS[5],100,T)
)

# select all column names where the column has more than 1 distinct values
my_df %>% 
  summarise_all(function(x) length(unique(x))) %>%
  gather() %>%
  filter(value > 1) %>%
  pull(key) -> list_cols

# apply function only to those columns
my_df %>% 
  select(list_cols) %>%
  summarise_all(funs(chisq.test(.,my_df$two_f)$p.value))

# # A tibble: 1 x 2
#     one_f                      two_f
#     <dbl>                      <dbl>
#   1 0.880 0.000000000000000000000120

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM