在 mutate() 之外以行方式使用 purrr::pmap()
Using purrr::pmap() in a rowwise manner outside of mutate()
我正在尝试使用 purrr::pmap() 沿某些数据框行以按行方式应用自定义函数。我可以使用 for 循环和 apply()
实现我想要的最终结果,但是当我尝试使用 pmap()
时,我只能结合 mutate() 获得我想要的结果,这在我的真实-life 应用案例将是不够的。
有没有办法使用 pmap()
来应用我的自定义函数,只打印输出而不是存储在新列中?
library(dplyr)
library(purrr)
library(tibble)
创建演示数据和自定义函数
set.seed(57)
ds_mt <-
mtcars %>%
rownames_to_column("model") %>%
mutate(
am = factor(am, labels = c("auto", "manual")),
vs = factor(vs, labels = c("V", "S"))
) %>%
select(model, mpg, wt, cyl, am, vs) %>%
sample_n(3)
foo <- function(model, am, mpg){
print(
paste("The", model, "has a", am, "transmission and gets", mpg, "mpgs.")
)
}
行向for循环的成功例子:
for (row in 1:nrow(ds_mt)) {
foo(
model = ds_mt[row, "model"],
am = ds_mt[row, "am"],
mpg = ds_mt[row, "mpg"]
)
}
使用apply()
的成功示例:
row.names(ds_mt) <- NULL # to avoid named vector as output
apply(
ds_mt,
MARGIN = 1,
FUN = function(ds)
foo(
model = ds["model"],
am = ds["am"],
mpg = ds["mpg"]
)
)
在 mutate()
中使用 pmap()
的示例几乎是我所需要的。
ds_mt %>%
mutate(new_var =
pmap(
.l =
list(
model = model,
am = am,
mpg = mpg
),
.f = foo
))
失败代码:为什么这不起作用?
ds_mt %>%
pmap(
.l =
list(
model = model,
am = am,
mpg = mpg
),
.f = foo
)
因此,经过更多阅读后,这似乎是 pwalk()
而不是 pmap()
的情况,因为我试图让输出打印(即副作用)而不是存储在数据框中。
library(dplyr)
library(purrr)
library(tibble)
set.seed(57)
ds_mt <-
mtcars %>%
rownames_to_column("model") %>%
mutate(
am = factor(am, labels = c("auto", "manual")),
vs = factor(vs, labels = c("V", "S"))
) %>%
select(model, mpg, wt, cyl, am, vs) %>%
sample_n(3)
foo <- function(model, am, mpg){
print(
paste("The", model, "has a", am, "transmission and gets", mpg, "mpgs.")
)
}
ds_mt %>%
select(model, am, mpg) %>%
pwalk(
.l = .,
.f = foo
)
我正在尝试使用 purrr::pmap() 沿某些数据框行以按行方式应用自定义函数。我可以使用 for 循环和 apply()
实现我想要的最终结果,但是当我尝试使用 pmap()
时,我只能结合 mutate() 获得我想要的结果,这在我的真实-life 应用案例将是不够的。
有没有办法使用 pmap()
来应用我的自定义函数,只打印输出而不是存储在新列中?
library(dplyr)
library(purrr)
library(tibble)
创建演示数据和自定义函数
set.seed(57)
ds_mt <-
mtcars %>%
rownames_to_column("model") %>%
mutate(
am = factor(am, labels = c("auto", "manual")),
vs = factor(vs, labels = c("V", "S"))
) %>%
select(model, mpg, wt, cyl, am, vs) %>%
sample_n(3)
foo <- function(model, am, mpg){
print(
paste("The", model, "has a", am, "transmission and gets", mpg, "mpgs.")
)
}
行向for循环的成功例子:
for (row in 1:nrow(ds_mt)) {
foo(
model = ds_mt[row, "model"],
am = ds_mt[row, "am"],
mpg = ds_mt[row, "mpg"]
)
}
使用apply()
的成功示例:
row.names(ds_mt) <- NULL # to avoid named vector as output
apply(
ds_mt,
MARGIN = 1,
FUN = function(ds)
foo(
model = ds["model"],
am = ds["am"],
mpg = ds["mpg"]
)
)
在 mutate()
中使用 pmap()
的示例几乎是我所需要的。
ds_mt %>%
mutate(new_var =
pmap(
.l =
list(
model = model,
am = am,
mpg = mpg
),
.f = foo
))
失败代码:为什么这不起作用?
ds_mt %>%
pmap(
.l =
list(
model = model,
am = am,
mpg = mpg
),
.f = foo
)
因此,经过更多阅读后,这似乎是 pwalk()
而不是 pmap()
的情况,因为我试图让输出打印(即副作用)而不是存储在数据框中。
library(dplyr)
library(purrr)
library(tibble)
set.seed(57)
ds_mt <-
mtcars %>%
rownames_to_column("model") %>%
mutate(
am = factor(am, labels = c("auto", "manual")),
vs = factor(vs, labels = c("V", "S"))
) %>%
select(model, mpg, wt, cyl, am, vs) %>%
sample_n(3)
foo <- function(model, am, mpg){
print(
paste("The", model, "has a", am, "transmission and gets", mpg, "mpgs.")
)
}
ds_mt %>%
select(model, am, mpg) %>%
pwalk(
.l = .,
.f = foo
)