library(ggmosaic)
library(purrr)
library(dplyr)
library(tibble)
library(tidyr)
library(broom)这个问题是Jake提供的上一个答案的扩展(链接如下)。
Function for Tidy chisq.test Output for Visualizing or Filtering P-Values
我想将下面的代码转换为一个使用标准求值的函数,这样我就可以跨不同的变量创建整洁的chisq.test结果。下面的代码在映射行中使用"happy$happy“来查找"happy”变量和其他分类变量之间的关联。该函数允许我将“幸福”更改为另一个变量,例如,“健康”或“婚姻”。
我还想在函数中包含最后一行“未嵌套”行,这样它就可以返回整齐的chisq.test结果。
df <- happy %>%
select(-id,-year,-age,-wtssall) %>%
map(~chisq.test(.x,happy$happy))%>%
tibble(names=names(.),data=.) %>%
mutate(stats=map(data,tidy))
unnest(df,stats)发布于 2017-02-25 15:40:50
您可以使用happy[,"happy"]替换happy$happy,这将允许您执行以下操作:
chifun <- function(var) {
df <- happy %>% select(-id,-year,-age,-wtssall)%>%
map(~chisq.test(.x,happy[,var]))%>%
tibble(names=names(.),data=.)%>%
mutate(stats=map(data,tidy)) %>% unnest(stats)
return(df)
}
chifun("happy")https://stackoverflow.com/questions/42407220
复制相似问题