【发布时间】:2021-12-07 10:42:52
【问题描述】:
我已阅读 [this][1] 和 [this][2],但无法使其适应我的用例。我正在尝试使用openxlsx 函数writeData 来编写我保存在向量中的几个数据帧以及工作簿工作表的名称,也保存在向量中:
names_of_worksheets <- c(
"total_suppliers",
"nato_fvey_suppliers",
"us_suppliers",
"sole_sourcing",
"single_sourcing",
"geographic_risk_us_only",
"foreign_dependence",
"exposure_to_non_nato_fvey"
)
names_of_dataframes <- c(
total_suppliers,
nato_fvey_suppliers,
us_suppliers,
sole_sourcing,
single_sourcing,
geographic_risk_us_only,
foreign_dependence,
exposure_to_non_nato_fvey
)
我想写的伪代码是一个循环遍历两个列表/向量的 for 循环(我想我可以在 python 中完成):
for (name_of_worksheet, name_of_dataframe in names_of_worksheets, names_of_data_frames) {
writeData(workbook, name_of_worksheet, name_of_dataframe)
}
这当然行不通。我已经尝试过map2,但遇到了一个我无法处理的错误:
map2(names_of_worksheets, names_of_dataframes, writeData, workbook)
Error: Mapped vectors must have consistent lengths:
* `.x` has length 8
* `.y` has length 25
有什么想法吗?谢谢! [1]:Looping over multiple lists with base R [2]:R Loop Iterating Over Two Lists
【问题讨论】:
-
您的工作簿对象是什么样的?它是一个 data.frames 列表?
标签: r for-loop mapping openxlsx