将变量传递到MAP功能公式中的.x $
第一个示例有效,但是我想沿示例2的行中移动更多的重复代码。我还没有找到正确的rlang方法来处理.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
由
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要使第二版中的代码工作,您的
slope
函数将需要获取数据参数以及您希望回归的列的名称。然后,列名称需要将其制成lm
调用的公式,可以通过几种方式完成,也许最容易使用一些字符串解析和as.formula
。为简单起见,我还将使用data
lm
的参数。因此,您有:
我个人认为,这样做的一种更强大,更通用的方法是传递一个公式:
To get the code working in the second version, your
slope
function will need to take a data argument as well as the name of the column you wish to regress. The column name then needs to make it into the formula of thelm
call, which can be done in a few ways, perhaps the easiest being with some string parsing andas.formula
. I would also use thedata
argument oflm
for simplicity.So that you have:
Personally, I think that a more robust and versatile way to do it is by passing a formula: