The first example works, but I'd like to move more of the repetitive code inside a function along the lines of example 2. I haven't found the right rlang approach to deal with the .x$x.
library(tidyverse)
# This produces the desired result:
slope <- function(y, x){
coef(lm(y ~ x))[2]
}
tribble(
~group, ~y, ~x1, ~x2,
"a", 1, 1, 4,
"a", 2, 2, 5,
"a", 3, 3, 6,
"b", 1, 4, 8,
"b", 2, 6, 12,
"b", 3, 8, 16
) |>
nest(data = -group) |>
mutate(
slope1 = map_dbl(data, ~ slope(.x$y, .x$x1)),
slope2 = map_dbl(data, ~ slope(.x$y, .x$x2))
)
#> # A tibble: 2 × 4
#> group data slope1 slope2
#> <chr> <list> <dbl> <dbl>
#> 1 a <tibble [3 × 3]> 1 1
#> 2 b <tibble [3 × 3]> 0.5 0.25
# I would like to use rlang to further simplify with something like this,
# but I can't see how to combine the variable with .x$:
slope <- function(x) {
map_dbl(data, ~ coef(lm(.x$y ~ .x$x))[2])
}
tribble(
~group, ~y, ~x1, ~x2,
"a", 1, 1, 4,
"a", 2, 2, 5,
"a", 3, 3, 6,
"b", 1, 4, 8,
"b", 2, 6, 12,
"b", 3, 8, 16
) |>
nest(data = -group) |>
mutate(
slope1 = slope(x1),
slope2 = slope(x2)
)
#> Error in `mutate()`:
#> ! Problem while computing `slope1 = slope(x1)`.
#> Caused by error in `purrr:::stop_bad_type()`:
#> ! `.x` must be a vector, not a function
Created on 2022-05-24 by the reprex package (v2.0.1)