【问题标题】:For loop for writing an excel file using xlsx?使用xlsx编写excel文件的for循环?
【发布时间】:2021-06-04 16:15:05
【问题描述】:

我正在尝试为您在下面的数据中看到的 STR_NBR 编写一个单独的 Excel 电子表格

换句话说,我认为逻辑如下:

for (i in seq_along(STR_NBR)) {
  openxlsx::write.xlsx("C:/Users/santi/Documents/R_Scripts/Export_Data_CSV.xlsx", 
                    output_file = sprintf("STR_NBR%s.xlsx", STR_NBR[i])
}

基本上,我正在尝试为每个 STR_NBR 创建一个单独的电子表格

示例:假设我想为您在下面看到的每个 type 列打印一个电子表格。

  set.seed(42)  ## for sake of reproducibility
    n <- 6
    dat <- data.frame(id=1:n, 
                      date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),
                      group=rep(LETTERS[1:2], n/2),
                      age=sample(18:30, n, replace=TRUE),
                      type=factor(paste("type", 1:n)),
                      x=rnorm(n))
    dat
    #   id       date group age   type         x
    # 1  1 2020-12-26     A  27 type 1 0.0356312
    # 2  2 2020-12-27     B  19 type 2 1.3149588
    # 3  3 2020-12-28     A  20 type 3 0.9781675
    # 4  4 2020-12-29     B  26 type 4 0.8817912
    # 5  5 2020-12-30     A  26 type 5 0.4822047
    # 6  6 2020-12-31     B  28 type 6 0.9657529

用我的实际数据框中的数据更新:我正在尝试为每个 MVNDR 列打印一个新的电子表格,但不是按每一行分组

Quote Date  eSVS Order Nbr  MVNDR
2021-05-24  H6328-206574    60710435
2021-05-27  H8926-157085    60710435
2021-05-24  H1020-178324    60660525
2021-05-24  H1020-178324    60660525
2021-05-27  H0772-64192 60074330
2021-05-27  H0772-64192 60074330
2021-05-27  H0772-64192 60074330
2021-05-25  H6646-240810    60063056
2021-05-25  H6646-240810    60063056

【问题讨论】:

  • 您能否向我们提供一些测试数据(而不是屏幕截图),我们可以复制和粘贴这些数据,以便更好地了解问题并测试可能的解决方案?您可以使用dput(YOUR_DATASET) 共享数据集,或者使用dput(head(YOUR_DATASET)) 共享较小的样本。 (有关详细说明,请参阅this answer。)
  • 用虹膜数据集更新
  • 你能举例说明输出的样子吗?每个电子表格是否只包含指定类型的行?
  • 它只是与 STR_NBR 关联的相关列和行
  • @Jaskeil 如果 MVNDR 中的值包含 /,则在尝试保存数据时可能会导致问题。您能否在数据帧上使用 dput 并将结果添加到问题中,以便我可以复制它并进一步调查?

标签: r for-loop dplyr xlsx openxlsx


【解决方案1】:

您可以使用dplyr 包中的group_bygroup_split 来拆分数据,然后使用xlsx 包中的write.xlsx 创建Excel 工作簿。

以下代码使用您的示例数据,您应该可以根据实际数据对其进行调整。

library(dplyr)
library(xlsx)

set.seed(42)  ## for sake of reproducibility
n <- 6
dat <- data.frame(id=1:n, 
                  date=seq.Date(as.Date("2020-12-26"), as.Date("2020-12-31"), "day"),
                  group=rep(LETTERS[1:2], n/2),
                  age=sample(18:30, n, replace=TRUE),
                  type=factor(paste("type", 1:n)),
                  x=rnorm(n))
dat
#   id       date group age   type         x
# 1  1 2020-12-26     A  27 type 1 0.0356312
# 2  2 2020-12-27     B  19 type 2 1.3149588
# 3  3 2020-12-28     A  20 type 3 0.9781675
# 4  4 2020-12-29     B  26 type 4 0.8817912
# 5  5 2020-12-30     A  26 type 5 0.4822047
# 6  6 2020-12-31     B  28 type 6 0.9657529

dat_grouped <- dat %>% group_by(type)

lapply(group_split(dat_grouped), function(x){write.xlsx(x,paste0(x$type, ".xlsx"))})

【讨论】:

  • 当我应用到我的数据框时,我遇到了以下问题:
  • Error in saveWorkbook(wb, file, password = password) : java.lang.NoSuchMethodError: &lt;init&gt; In addition: Warning messages: 1: In if (type == "xls") { : the condition has length &gt; 1 and only the first element will be used 2: In if (type == "xlsx") { : the condition has length &gt; 1 and only the first element will be used
  • 什么代码给出了错误?数据有什么“不同”吗?
  • 感谢您的回复,代码如下:lapply(group_split(d22_open_quotes), function(x){write.xlsx(x,paste0(x$MVNDR, ".xlsx"))}) 我尝试为每个 mvndr 打印一个新的电子表格。让我也用数据框更新问题
  • 我重新构建了问题,使其更清晰,所需的输出也更清晰stackoverflow.com/questions/67873269/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
相关资源
最近更新 更多