Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将管道输出直接传递给purrr映射函数_R_Tidyverse_Purrr - Fatal编程技术网

将管道输出直接传递给purrr映射函数

将管道输出直接传递给purrr映射函数,r,tidyverse,purrr,R,Tidyverse,Purrr,这个问题与我先前的问题一致。但是,我尝试将管道调用的输出与purr的map函数组合到一个管道中。例如: library(tidyverse) library(purrr) my_tbl <- tibble(a = rep(c(0, 1), each = 5), b = rep(c(0, 1), times = 5), c = runif(10), d = rexp(10)) %>% mutate_

这个问题与我先前的问题一致。但是,我尝试将管道调用的输出与
purr
的map函数组合到一个管道中。例如:

library(tidyverse)
library(purrr)

my_tbl <- tibble(a = rep(c(0, 1), each = 5),
             b = rep(c(0, 1), times = 5),
             c = runif(10),
             d = rexp(10)) %>%
    mutate_at(vars(1,2), as.factor)

map(names(my_tbl)[-1], ~glm(reformulate(.x, "a"), data = my_tbl, family = "binomial")) %>% summary()

在这种情况下,您不需要purrr:

custom_fun <- function(x) {
  glm(reformulate(names(x)[-1], "a"), data = x, family = "binomial") %>% 
    summary
}

my_tbl <- tibble(a = rep(c(0, 1), each = 5),
                 b = rep(c(0, 1), times = 5),
                 c = runif(10),
                 d = rexp(10)) %>%
  mutate_at(vars(1,2), as.factor) %>% 
  custom_fun()
custom\u fun%
总结
}
我的tbl%
在(变量(1,2),as.factor)%>
自订
您可以将purrr与以下内容一起使用:

my_tbl <- tibble(a = rep(c(0, 1), each = 5),
                 b = rep(c(0, 1), times = 5),
                 c = runif(10),
                 d = rexp(10)) %>%
  mutate_at(vars(1,2), as.factor) %>% 
  nest(data = everything()) %>% 
  mutate(res = map(data, custom_fun))
my\u tbl%
在(变量(1,2),as.factor)%>
嵌套(数据=一切())%>%
变异(res=map(数据,自定义)

非常小,但是
purr
已经通过加载
tidyverse
加载了,所以您不需要显式地这样做。或者,更好的是,只需加载所需的tidyverse包,这样就可以减少开销
custom_fun <- function(x) {
  glm(reformulate(names(x)[-1], "a"), data = x, family = "binomial") %>% 
    summary
}

my_tbl <- tibble(a = rep(c(0, 1), each = 5),
                 b = rep(c(0, 1), times = 5),
                 c = runif(10),
                 d = rexp(10)) %>%
  mutate_at(vars(1,2), as.factor) %>% 
  custom_fun()
my_tbl <- tibble(a = rep(c(0, 1), each = 5),
                 b = rep(c(0, 1), times = 5),
                 c = runif(10),
                 d = rexp(10)) %>%
  mutate_at(vars(1,2), as.factor) %>% 
  nest(data = everything()) %>% 
  mutate(res = map(data, custom_fun))