【问题标题】:Merge Excel spreadsheets into just one in R将 Excel 电子表格合并为 R 中的一个
【发布时间】:2022-01-22 20:28:51
【问题描述】:

我有 4 个单独的 Excel 电子表格,我想将它们统一起来,也就是说,只保留一个。有可能的?事实上,我是这样做的:

library(readxl)

x<-read_excel('C:/Users/Jose/Desktop/Calculation 1.xlsx',sheet = "Calculation 1")
y<-read_excel('C:/Users/Jose/Desktop/Calculation 2.xlsx',sheet = "Calculation 2")
z<-read_excel('C:/Users/Jose/Desktop/Calculation 3.xlsx',sheet = "Calculation 3")
w<-read_excel('C:/Users/Jose/Desktop/Calculation 4.xlsx',sheet = "Calculation 4")

我想将其转换为一个电子表格,这样它将有 4 个选项卡,计算 1、计算 2、计算 3 和计算 4。

【问题讨论】:

    标签: r excel


    【解决方案1】:

    你可以试试-

    library(readxl)
    #Return file path for the file which has "Calculation" in it's name and ends with `xlsx`. 
    filenames <- list.files("C:/Users/Jose/Desktop/", pattern = 'Calculation.*\\.xlsx$', full.names = TRUE)
    
    #Read the data in a list
    list_data <- lapply(filenames, read_excel)
    #Name the list with the filename without extension
    names(list_data) <- tools::file_path_sans_ext(basename(filenames))
    #Write a new excel file with all the tabs. 
    writexl::write_xlsx(list_data, 'Calculation.xlsx')
    

    【讨论】:

    • 感谢您的回复!只有一件事:例如,如果我有不同名称的工作表,例如:计算、测试、零件和软件。 filenames 会是什么样子?
    • 如果每个文件中只有一张纸,代码中没有任何变化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 2013-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多