【问题标题】:Function for Tidying Chisq.Test Output with Standard Evaluation使用标准评估整理 Chisq.Test 输出的功能
【发布时间】:2017-02-23 04:43:50
【问题描述】:
library(ggmosaic)
library(purrr)
library(dplyr)
library(tibble)
library(tidyr)
library(broom)

此问题是对 Jake Kaupp 提供的先前答案的扩展(下面的链接)。

Function for Tidy chisq.test Output for Visualizing or Filtering P-Values

我想将下面的代码转换为使用标准评估的函数,以便我可以跨不同变量创建整齐的 chisq.test 结果。下面的代码在地图行中使用“happy$happy”来查找“happy”变量和其他分类变量之间的关联。该函数允许我将“快乐”更改为另一个变量,例如“健康”或“婚姻”。

我还想在函数中包含最后一个“unnest”行,以便它返回整洁的 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)

【问题讨论】:

  • 我尝试过类似 - Fun%select(-id,-year,-age,-wtssall)%>%map(~chisq.test(. x, Var)%>%tibble(names=names(.),data=.)%>%mutate(stats=map(data,tidy))。我还是标准评估的新手,不知道如何处理purrr::map 函数与 SE 以及“happy$happy”部分一起使用?我在此评论中尝试了示例的变体,但无法正常工作。我知道 dplyr 动词有 SE 版本,但不确定地图还是小标题?
  • 当我之前将 SE 与 tidyr 和 dplyr 一起使用时,我使用了 SE 动词,例如,gather 变成了gather_,count 变成了 count_,等等,并为动态变量使用了引号。但是对于这个问题,我不太确定 map 和 tibble 作为 SE,或者在哪里或引用什么?
  • 对于第一条评论,我是想写这个...我忘记了 { } Fun%select(-id,-year,-age,- wtssall)%>%map(~chisq.test(.x, Var)%>%tibble(names=names(.),data=.)%>%mutate(stats=map(data,tidy))}.
  • 将您尝试的内容添加到问题中。作为评论无法阅读。

标签: r purrr tidyverse


【解决方案1】:

您可以将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")

【讨论】:

  • 谢谢,看来赏金是你的。我已经习惯了使用“$”,以至于忘记了“[]”。我有一个问题,为什么我不必在这个函数中使用任何标准评估?在以前的函数中,我不得不使用动词的 SE 版本,例如“gather_”或“count_”,以及引号等。为什么在这个例子中这些都不是必需的?
  • 这是因为您没有将任何列名作为字符串传递给任何 tidyverse 函数。
  • 另外,我注意到您使用了“df
  • 最后,最后一个问题比较大,所以如果你想让我为它创建一个新问题,这样你就可以因为提供答案而获得荣誉,请告诉我,但我会在这里包含它无论如何在评论中。我如何使用“return”返回两件事——整洁的小标题和情节?此代码不起作用: hifun%select(-id,-year,-age,-wtssall)%>% + map(~chisq.test(.x ,happy[,var]))%>% + tibble(names=names(.),data=.)%>% + mutate(stats=map(data,tidy))%>%unnest(stats) + GG
  • Chifun%select(-id,-year,-age,-wtssall)%>% map(~chisq.test(.x, happy[,var]))%>% tibble(names=names(.),data=.)%>% mutate(stats=map(data,tidy))%>%unnest(stats) GG
猜你喜欢
  • 1970-01-01
  • 2020-11-29
  • 1970-01-01
  • 2015-03-10
  • 1970-01-01
  • 2015-02-27
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多