Reputation: 3839
This question is in line with my previous question here. However, I try to combine the output of the pipe call with purrr
's map function into a single pipeline. For example:
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()
I tried with
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(.)[-1], ~glm(reformulate(.x, "a"), data = ., family = "binomial")) %>% summary()}
but I got:
Error in eval(predvars, data, env) :
invalid 'envir' argument of type 'character'
Upvotes: 0
Views: 252
Reputation: 1258
You do not need purrr in this case:
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()
You could use purrr with the following:
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))
Upvotes: 1