使用 R 循环更改名称的多个 excel 文件

Looping multiple excel files with changing names using R

我有多个 excel 文件,名为“2003_BY_HR.xls 的副本”、“2004_BY_HR.xls 的副本”...“2010_BY_HR.xls 的副本”等。文件命名是独一无二的,只有名字的年份变化。工作表也具有相同的名称。我正在使用 readxl 包来读取数据。我可以通过简单地更改年份来读取和输出每个文件的数据。我想自动有效地实现这一点,而不是手动更改文件名并重新 运行 脚本。我的有效脚本如下所示。

setwd("Data")
library(readxl)

# I read in the first file
dataf<-"Copy of 2003_BY_HR.xls"
ICA <- read_excel(dataf, 
              sheet = "ICA_HR_2003")
ET  <- read_excel(dataf, 
              sheet = "ET_HR_2003", na ="0")

# I read the data from first sheet and retrieve variable of interest
Grain_ICA         <- ICA$`Grain ICA`
Rice_ICA          <- ICA$`Rice ICA`
Cotton_ICA        <- ICA$`Cotton ICA`

# I read the data from second sheet and retrieve variable of interest
Grain_ET         <-ET$`Grain ET WA`
Rice_ET          <-ET$`Rice ET WA`
Cotton_ET        <-ET$`Cotton ET WA`

# I compute the results and save to a single file by appending
ET_grain  <- sum(Grain_ICA * Grain_ET * 1000, na.rm=T)
ET_rice   <- sum(Rice_ICA * Rice_ET *1000, na.rm=T)
ET_cotton <- sum(Cotton_ICA * Cotton_ET *1000, na.rm=T)

result <- data.frame( ET_grain,ET_rice,ET_cotton)
colnames(result) <- c("Grain","Rice","Cotton")
write.table(result, file = "ET.csv", append=T, sep=',', 
        row.names=F, 
        col.names=F)

我创建了一个向量,其中需要将所有年份用作文件名的一部分。然后我将您所做的更改为可以在其中指定输入年份的函数。最后,我在创建的向量中遍历所有年份:

setwd("Data")
library(readxl)

# here you need to list all years that appear in the names of the files
years <- c("2003", "2004")


myFunction <- function(.year){
  # I read in the first file
  dataf <- paste0("Copy of ", .year, "_BY_HR.xls")
  ICA <- read_excel(dataf, 
                    sheet = paste0("ICA_HR_", .year)
  ET  <- read_excel(dataf, 
                    sheet = paste0("ET_HR_", .year), na ="0")
  
  # I read the data from first sheet and retrieve variable of interest
  Grain_ICA         <- ICA$`Grain ICA`
  Rice_ICA          <- ICA$`Rice ICA`
  Cotton_ICA        <- ICA$`Cotton ICA`
  
  # I read the data from second sheet and retrieve variable of interest
  Grain_ET         <-ET$`Grain ET WA`
  Rice_ET          <-ET$`Rice ET WA`
  Cotton_ET        <-ET$`Cotton ET WA`
  
  # I compute the results and save to a single file by appending
  ET_grain  <- sum(Grain_ICA * Grain_ET * 1000, na.rm=T)
  ET_rice   <- sum(Rice_ICA * Rice_ET *1000, na.rm=T)
  ET_cotton <- sum(Cotton_ICA * Cotton_ET *1000, na.rm=T)
  
  result <- data.frame( ET_grain,ET_rice,ET_cotton)
  colnames(result) <- c("Grain","Rice","Cotton")
  write.table(result, file = "ET.csv", append=T, sep=',', 
              row.names=F, 
              col.names=F)
}

# this loops through all the years
for (year in seq_along(years)) {
  myFunction(year)
}

你应该使用list.files来制作一个输入文件列表,你可以使用模式来匹配文件名。然后将上面的所有内容放入一个函数中,然后使用 lapply 将该函数应用于文件名列表。

setwd("Data")
library(readxl)

# I read in the first file
files.lst <- list.files("./", pattern = "Copy of .*\.xls")
files.lst


MyFun <- function(x) {
  dataf <- x
  ICA <- read_excel(dataf, 
                    sheet = "ICA_HR_2003")
  ET  <- read_excel(dataf, 
                    sheet = "ET_HR_2003", na ="0")

  # I read the data from first sheet and retrieve variable of interest
  Grain_ICA         <- ICA$`Grain ICA`
  Rice_ICA          <- ICA$`Rice ICA`
  Cotton_ICA        <- ICA$`Cotton ICA`

  # I read the data from second sheet and retrieve variable of interest
  Grain_ET         <-ET$`Grain ET WA`
  Rice_ET          <-ET$`Rice ET WA`
  Cotton_ET        <-ET$`Cotton ET WA`

  # I compute the results and save to a single file by appending
  ET_grain  <- sum(Grain_ICA * Grain_ET * 1000, na.rm=T)
  ET_rice   <- sum(Rice_ICA * Rice_ET *1000, na.rm=T)
  ET_cotton <- sum(Cotton_ICA * Cotton_ET *1000, na.rm=T)

  colnames(result) <- c("Grain","Rice","Cotton")
  result.file <- paste0(basename(dataf), ".result.csv")
  write.table(result, file = result.file, append=T, sep=',', 
              row.names=F, 
              col.names=F)

}

res <- lapply(files.lst, MyFun)

通常,我会创建一个列表对象,return 从函数中得到结果,而不是在该函数中保存文件。 data.table 是一个很棒的包,它有一个名为 rbindlist 的方法,可用于将您的结果列表转换为单个 table,尤其是当它们具有相同的列时。 ggplot 也易于使用。