首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Purrr::Invoke()与Table和Chisq.Test一起改进工作流

使用Purrr::Invoke()与Table和Chisq.Test一起改进工作流
EN

Stack Overflow用户
提问于 2017-03-18 04:53:19
回答 1查看 103关注 0票数 2
代码语言:javascript
复制
library(purrr)
library(tidyverse)

我正在努力更好地理解purrr::invoke()函数。我觉得我错过了一些简单的东西,可以帮助我的工作流程。我承认我对"do.call“缺乏理解,因为"invoke”是包装器。

例如,我使用的是来自happy包的“ggmosaic”数据集,但是任何具有各种因素列的数据集都可以工作。

我试图使用"invoke“在选定的因素列上运行一些表,但没有成功。我只想根据元素列名列表生成一些简单的表。

就像这样..。

代码语言:javascript
复制
happy%>%invoke(table,list(c("health",c("happy"))

happy%>%select_if(is.factor)%>%invoke(table)

L <- list("health","happy","degree")
happy%>%invoke(table,L)

我还想知道如何以其他方式将purrr::invoke()集成到我的工作流中,例如,使用chisq.test

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-23 17:45:31

如果您想在dataframe的行上迭代一个函数,我建议您考虑使用transpose + map而不是invoke

代码语言:javascript
复制
library(ggmosaic) 
library(purrr)
library(tidyverse) 

data(happy)

h <- 
  happy %>% 
  as_tibble %>% 
  sample_n(100)  ## use a subset for the example

h %>% 
  select_if(is.factor) %>% 
  transpose() %>% 
  map(table) %>% 
  mutate(h, TABLE = .) %>%  
  select(TABLE, everything())
#> # A tibble: 100 × 11
#>          TABLE    id         happy  year   age    sex       marital
#>         <list> <dbl>        <fctr> <dbl> <dbl> <fctr>        <fctr>
#> 1  <S3: table>   417    very happy  1975    68 female       married
#> 2  <S3: table>   529  pretty happy  1983    20 female       married
#> 3  <S3: table>   226  pretty happy  1973    32 female       married
#> 4  <S3: table>  1327 not too happy  1974    39   male       married
#> 5  <S3: table>  1632  pretty happy  1976    40   male      divorced
#> 6  <S3: table>    46 not too happy  1982    31 female       married
#> 7  <S3: table>   729  pretty happy  1994    85 female       widowed
#> 8  <S3: table>   557  pretty happy  1985    47 female      divorced
#> 9  <S3: table>   136  pretty happy  1978    48 female       married
#> 10 <S3: table>  1020    very happy  2000    21   male never married
#> # ... with 90 more rows, and 4 more variables: degree <fctr>,
#> #   finrela <fctr>, health <fctr>, wtssall <dbl>

但是这个结果似乎不太有用--也许您可以将您的总结map到经过修剪的数据集?

代码语言:javascript
复制
h %>% select_if(is.factor) %>% map(table)
#> $happy
#> 
#> not too happy  pretty happy    very happy 
#>             9            50            30 
#> 
#> $sex
#> 
#>   male female 
#>     53     47 
#> 
#> $marital
#> 
#>       married never married      divorced       widowed     separated 
#>            53            25            14             7             0 
#> 
#> $degree
#> 
#> lt high school    high school junior college       bachelor       graduate 
#>             25             44              6             16              8 
#> 
#> $finrela
#> 
#> far below average     below average           average     above average 
#>                 6                25                41                16 
#> far above average 
#>                 0 
#> 
#> $health
#> 
#>      poor      fair      good excellent 
#>         4        13        41        18
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42870610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档