如何将多个列表列表作为参数传递给 R 中的 mapply 函数
How to pass multiple list of lists as arguments to mapply function in R
给定三个列表列表(avg_errors
、year_months
、dfs
)如下:
avg_errors <- list(0.7, 0.92, 2.35)
输出:
[[1]]
[1] 0.7
[[2]]
[1] 0.92
[[3]]
[1] 2.35
和:
year_months <- list(list('2021-12'), list('2021-11'), list('2021-10'))
和数据dfs
:
dfs <- list(structure(list(id = c("M01", "M02", "S01"), `2021-10(actual)` = c(8.9,
15.7, 5.3), `2021-11(actual)` = c(7.3, 14.8, 3.1), `2021-12(actual)` = c(6.1,
14.2, 3.5), `2021-12(pred)` = c(6.113631596, 14.16243166, 3.288372517
), `2021-12(error)` = c(0.719846116, 0.154463985, 1.225992602
), mean_col = c(7.43333333333333, 14.9, 3.96666666666667), act_direction = c(-1.2,
-0.600000000000001, 0.4), pred_direction = c(-1.186368404, -0.637568340000001,
0.188372517)), row.names = c(NA, -3L), class = "data.frame"),
structure(list(id = c("M01", "M02", "S01"), `2021-09(actual)` = c(10.3,
17.3, 6.4), `2021-10(actual)` = c(8.9, 15.7, 5.3), `2021-11(actual)` = c(7.3,
14.8, 3.1), `2021-11(pred)` = c(8.352097939, 13.97318204,
3.164682627), `2021-11(error)` = c(1.998109138, 0.414373304,
0.342072615), mean_col = c(8.83333333333333, 15.9333333333333,
4.93333333333333), act_direction = c(-1.6, -0.899999999999999,
-2.2), pred_direction = c(-0.547902061, -1.72681796, -2.135317373
)), row.names = c(NA, -3L), class = "data.frame"), structure(list(
id = c("M01", "M02", "S01"), `2021-08(actual)` = c(12.6,
19.2, 8.3), `2021-09(actual)` = c(10.3, 17.3, 6.4), `2021-10(actual)` = c(8.9,
15.7, 5.3), `2021-10(pred)` = c(9.619846116, 15.54553601,
6.525992602), `2021-10(error)` = c(0.945567783, 4.883250027,
1.215819585), mean_col = c(10.6, 17.4, 6.66666666666667
), act_direction = c(-1.4, -1.6, -1.1), pred_direction = c(-0.680153884000001,
-1.75446399, 0.125992601999999)), row.names = c(NA, -3L
), class = "data.frame"))
如何将这些变量作为参数传递给函数 Plotting()
,如下所示:
library(gt)
library(tidyverse)
Plotting <- function(data, year_months, errors){
for (current_month in year_months){
for (avg_error in errors){
p <- data %>%
gt() %>%
tab_header(
title = gt::html(glue("<span style='color:red'>data for {current_month}</span>")),
subtitle = md(paste0("The average error for this month is: <em>", avg_error, '</em>.'))
)
return (p)
}
}
}
mapply(Plotting, dfs, year_months, errors)
当前代码给我:Error in (function (data, errors) : argument no use (dots[[3]][[1]])
.
如何调用函数并传递参数?感谢您的提前帮助。
我不确定这是否是您想要的结果,但我的猜测是,您不需要 Plotting
函数中的 for-loop,因为您已经在循环用 mapply
.
覆盖你的输入
library(gt)
library(tidyverse)
library(glue)
Plotting <- function(data, year_months, errors){
p <- data %>%
gt() %>%
tab_header(
title = gt::html(glue("<span style='color:red'>data for {year_months}</span>")),
subtitle = md(paste0("The average error for this month is: <em>", errors, '</em>.'))
)
print(p)
}
mapply(Plotting, dfs, year_months, avg_errors)
由 reprex package (v0.3.0)
创建于 2022-01-23
给定三个列表列表(avg_errors
、year_months
、dfs
)如下:
avg_errors <- list(0.7, 0.92, 2.35)
输出:
[[1]]
[1] 0.7
[[2]]
[1] 0.92
[[3]]
[1] 2.35
和:
year_months <- list(list('2021-12'), list('2021-11'), list('2021-10'))
和数据dfs
:
dfs <- list(structure(list(id = c("M01", "M02", "S01"), `2021-10(actual)` = c(8.9,
15.7, 5.3), `2021-11(actual)` = c(7.3, 14.8, 3.1), `2021-12(actual)` = c(6.1,
14.2, 3.5), `2021-12(pred)` = c(6.113631596, 14.16243166, 3.288372517
), `2021-12(error)` = c(0.719846116, 0.154463985, 1.225992602
), mean_col = c(7.43333333333333, 14.9, 3.96666666666667), act_direction = c(-1.2,
-0.600000000000001, 0.4), pred_direction = c(-1.186368404, -0.637568340000001,
0.188372517)), row.names = c(NA, -3L), class = "data.frame"),
structure(list(id = c("M01", "M02", "S01"), `2021-09(actual)` = c(10.3,
17.3, 6.4), `2021-10(actual)` = c(8.9, 15.7, 5.3), `2021-11(actual)` = c(7.3,
14.8, 3.1), `2021-11(pred)` = c(8.352097939, 13.97318204,
3.164682627), `2021-11(error)` = c(1.998109138, 0.414373304,
0.342072615), mean_col = c(8.83333333333333, 15.9333333333333,
4.93333333333333), act_direction = c(-1.6, -0.899999999999999,
-2.2), pred_direction = c(-0.547902061, -1.72681796, -2.135317373
)), row.names = c(NA, -3L), class = "data.frame"), structure(list(
id = c("M01", "M02", "S01"), `2021-08(actual)` = c(12.6,
19.2, 8.3), `2021-09(actual)` = c(10.3, 17.3, 6.4), `2021-10(actual)` = c(8.9,
15.7, 5.3), `2021-10(pred)` = c(9.619846116, 15.54553601,
6.525992602), `2021-10(error)` = c(0.945567783, 4.883250027,
1.215819585), mean_col = c(10.6, 17.4, 6.66666666666667
), act_direction = c(-1.4, -1.6, -1.1), pred_direction = c(-0.680153884000001,
-1.75446399, 0.125992601999999)), row.names = c(NA, -3L
), class = "data.frame"))
如何将这些变量作为参数传递给函数 Plotting()
,如下所示:
library(gt)
library(tidyverse)
Plotting <- function(data, year_months, errors){
for (current_month in year_months){
for (avg_error in errors){
p <- data %>%
gt() %>%
tab_header(
title = gt::html(glue("<span style='color:red'>data for {current_month}</span>")),
subtitle = md(paste0("The average error for this month is: <em>", avg_error, '</em>.'))
)
return (p)
}
}
}
mapply(Plotting, dfs, year_months, errors)
当前代码给我:Error in (function (data, errors) : argument no use (dots[[3]][[1]])
.
如何调用函数并传递参数?感谢您的提前帮助。
我不确定这是否是您想要的结果,但我的猜测是,您不需要 Plotting
函数中的 for-loop,因为您已经在循环用 mapply
.
library(gt)
library(tidyverse)
library(glue)
Plotting <- function(data, year_months, errors){
p <- data %>%
gt() %>%
tab_header(
title = gt::html(glue("<span style='color:red'>data for {year_months}</span>")),
subtitle = md(paste0("The average error for this month is: <em>", errors, '</em>.'))
)
print(p)
}
mapply(Plotting, dfs, year_months, avg_errors)
由 reprex package (v0.3.0)
创建于 2022-01-23