gpt4 book ai didi

r - 使用 purrr :walk2 时找不到对象 .x

转载 作者:行者123 更新时间:2023-12-05 03:59:58 27 4
gpt4 key购买 nike

我正在尝试在 purrr::walk2 处抛出一个函数,即:

library(stats19)
library(purrr)

walk2(.x = 2017, .y = "Accidents", .f = dl_stats19(year = .x, type = .y, data_dir = "./data", file_name = paste0("stats19_", .x, "_", .y), ask = FALSE))

# Error in dl_stats19(year = .x$years, type = types, data_dir = data_dir, : object '.x' not found

我不明白为什么 .x 没有在函数 dl_stats19()

中被提取

最佳答案

有几个可能的解决方案。第一种也是最简单的方法是在函数名称前包含波浪号,如下所示:

walk2(.x = 2017, 
.y = "Accidents",
.f = ~ dl_stats19(year = .x,
type = .y,
data_dir = "./data",
file_name = paste0("stats19_", .x, "_", .y),
ask = FALSE))

您还可以使用匿名函数并按位置匹配参数,如下所示:

walk2(.x = 2017, 
.y = "Accidents",
.f = function(a, b) dl_stats19(year = a,
type = b,
data_dir = "./data",
file_name = paste0("stats19_", a, "_", b),
ask = FALSE))

关于r - 使用 purrr :walk2 时找不到对象 .x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56704009/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com