首页 > 解决方案 > 编写一个 for 循环以在文件夹中的文件中执行相同的功能

问题描述

当我在文件上运行函数 nwc 时,它如图所示工作,但我想要一个 for 循环来调用文件夹中的所有文件来执行 nwc 函数。这没用。任何帮助将不胜感激。

nwc(nxmar_18)

#$Alloc_Matrix
# A tibble: 3 x 4
#     D1    D2    D3    D4
#  <dbl> <dbl> <dbl> <dbl>
#1     6     5     0     0
#2     0     5     8     0
#3     0     0     4    15

#$Total_Cost
#    D1
#1 1095    

不在 for 循环中工作。显示此错误

cost_aloc <- vector("double", length(mar_18))
for(p in mar_18){
  nxmar_18 <- read_excel(p)
  nxmar_18 <- nxmar_18[, -1]
  cost_aloc[p] <- nwc(nxmar_18[[p]])
  write.xlsx(cost_aloc, file = "out_put_8.xlsx")
  print(cost_aloc)
}

新名称:* `` -> ...1 -nrow(ex_matrix) 中的错误:一元运算符的参数无效

标签: rfor-looplinear-programming

解决方案


推荐阅读