使用 Purrr::Invoke() 与 Table 和 Chisq.Test 改进工作流程
Using Purrr::Invoke() with Table and Chisq.Test to Improve Workflow
library(purrr)
library(tidyverse)
我正在尝试更好地理解 purrr::invoke()
函数。我觉得我缺少一些简单的东西可以帮助我的工作流程。我承认我对“do.call
”缺乏理解,“invoke
”是一个包装器。
例如,我使用 ggmosaic
包中的“happy
”数据集,但任何具有各种因子列的数据集都可以使用。
我正在尝试将“invoke
”用于 运行 选定因子列上的某些表,但没有成功。我只想根据因子列名列表生成一些简单的表。
像这样...
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
。
如果你想在数据帧的行上迭代一个函数,我建议你考虑 transpose
+ map
而不是 invoke
:
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
你对修剪后的数据集的总结?
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
library(purrr)
library(tidyverse)
我正在尝试更好地理解 purrr::invoke()
函数。我觉得我缺少一些简单的东西可以帮助我的工作流程。我承认我对“do.call
”缺乏理解,“invoke
”是一个包装器。
例如,我使用 ggmosaic
包中的“happy
”数据集,但任何具有各种因子列的数据集都可以使用。
我正在尝试将“invoke
”用于 运行 选定因子列上的某些表,但没有成功。我只想根据因子列名列表生成一些简单的表。
像这样...
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
。
如果你想在数据帧的行上迭代一个函数,我建议你考虑 transpose
+ map
而不是 invoke
:
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
你对修剪后的数据集的总结?
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