【问题标题】:Looping multiple excel files with changing names using R使用R循环更改名称的多个excel文件
【发布时间】:2021-02-03 15:15:39
【问题描述】:

我有多个名为“2003_BY_HR.xls 的副本”、“2004_BY_HR.xls 的副本”...“2010_BY_HR.xls 的副本”等的 excel 文件。文件命名是唯一的,名称中只有年份更改。工作表也具有相同的名称。我正在使用 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)

【问题讨论】:

    标签: r excel loops append multiple-file-upload


    【解决方案1】:

    您应该使用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)
    

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

    【讨论】:

    • @Vincent,您建议用paste0("ICA_HR_2003") 替换"ICA_HR_2003" 没有任何区别,它是一回事。而且您的paste0("ICA_HR_2003", na="0") 看起来不正确。 paste0 函数没有 na 选项。如果您在读取 ​​Excel 工作表时遇到错误,这可能是因为您的 Excel 工作表名称不同,或者工作表名称中的尾随或前导空格。尝试在dataf &lt;- x 之后添加print(dataf) 以查看导致问题的文件并仔细检查文件名和工作表名。
    【解决方案2】:

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

    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)
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多