有几个类似的问题可以抓取chi-square
结果,但这解决了我的问题。我想为chi-square
中所有列的data_frame
测试计算p.values,并将其存储在原始data_frame
中的列中。会有重复的值,我很好。最后,我希望select
data_frame
中所有列的p.value都低于x,并且我的选择变量。
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))
给我这个错误:
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))
给我这个错误:
Error in n_distinct > 1 :
comparison (6) is possible only for atomic and list types
我正在寻找类似的东西。
my_df %>%
mutate(p.value = sample(c(0.043,0.87,0.00),nrow(.),T)) %>%
head()
然后我计划使用gather
和filter
然后spread
根据我的chi-square
测试获取显着关联的变量。
我想
my_df %>% filter(foo,bar >= 0.05)#function that finds p.values and filters by
# alpha level
将是我的最终目标。
答案 0 :(得分:1)
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