如何设计一个函数来读取多个excel文件并在R中进行重复计算

时间:2017-09-06 10:15:15

标签: r readxl

我有一个包含多个Excel工作表的文件,我想运行一个函数来读取每个工作表并从另一个工作表中减去一个列,然后计算一个平均值

我试过

library(readxl)

average_working_hours <- function(Name) {
    Name <- read_excel("~/Name.xlsx")
    hours12 <- 12*60*60
    av_wh_Name <- mean((Name$`Departure of staff`+ hours12) - Name$`Attendance of Staff`, na.rm = TRUE)
    av_wh_Name
}

average_working_hours(Name = Noha)

have an ERROR 
 **Error in read_fun(path = path, sheet = sheet, limits = limits, shim = shim,  : 
  Evaluation error: zip file 'C:/Users/user 2/Documents/Name.xlsx' cannot be opened.**

然后我试了

average_working_hours <- function(Name) {
    Name <- read_excel(sprintf("~/%s.xlsx ",Name))
    hours12 <- 12*60*60
    av_wh_Name <- mean((Name$`Departure of staff`+ hours12) - Name$`Attendance of Staff`, na.rm = TRUE)
    av_wh_Name
}
average_working_hours(Name = Noha)

have an ERROR 
 **Error in switch(ext, xls = "xls", xlsx = "xlsx", xlsm = "xlsx", if (nzchar(ext)) { : 
  EXPR must be a length 1 vector**

问题出在哪里?

1 个答案:

答案 0 :(得分:1)

在第二个代码中尝试使用average_working_hours(Name = "Noha")调用该函数。 Screenshot of the working code