将多个文件与不同的工作表名称组合到数据框架中

发布于 2025-02-02 22:03:25 字数 709 浏览 1 评论 0原文

我正在尝试结合两个功能。

第一个函数可以读取和导入每个 *.xlsx文件中的单独文件夹

library(readxl)
library(tidyverse)
file.list <- list.files(pattern='*.xlsx')
df.list <- lapply(file.list, read_excel)

,而第二个功能可以使用多个工作表工作表读取单个文件,并将A组合到一个数据框架中。

# set the working directory 
setwd(path)
  
# accessing all the sheets 
sheet = excel_sheets("Gfg.xlsx")
  
# applying sheet names to dataframe names
data_frame = lapply(setNames(sheet, sheet), 
                    function(x) read_excel("Gfg.xlsx", sheet=x))
  
# attaching all dataframes together
data_frame = bind_rows(data_frame, .id="Sheet")

因此,任何人都可以帮助我如何在一个或多个中合并这两个功能,以在第一个功能中添加可能的可能性,以将表格名称应用于数据帧名称,然后在数据框架中附加在一起?

I am trying to combine two functions.

The first function can read and import each *.xlsx file into a separate folder

library(readxl)
library(tidyverse)
file.list <- list.files(pattern='*.xlsx')
df.list <- lapply(file.list, read_excel)

While the second function can read the single file with multiple sheets worksheets and combine a into one data frame.

# set the working directory 
setwd(path)
  
# accessing all the sheets 
sheet = excel_sheets("Gfg.xlsx")
  
# applying sheet names to dataframe names
data_frame = lapply(setNames(sheet, sheet), 
                    function(x) read_excel("Gfg.xlsx", sheet=x))
  
# attaching all dataframes together
data_frame = bind_rows(data_frame, .id="Sheet")

So, can anybody help me how to merge these two functions in one or more precisely to add possibility in the first function to applying sheet names to dataframe names and then to attaching together in data frame?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她如夕阳 2025-02-09 22:03:25

你需要这个吗?

library(readxl)
library(tidyverse)

setwd("/Users/harshvardhan/Desktop/Dump/codes/sheets/")

read_dfs = function()
{
   # this has to initilised intelligently based on what the variables are
   df_complete = tibble(var1 = numeric(),
                        var2 = numeric(),
                        var3 = numeric())
      
   # listing all files in the directory
   file.list = list.files(pattern='*.xlsx')
   
   # first loop for all files
   for (book in file.list)
   {
      # getting sheets in this workbook
      sheet = excel_sheets(book)
      
      # second loop for all sheets
      for (s in sheet)
      {
         df = read_excel(path = book, sheet = s)
         df_complete = rbind(df_complete, df)
      }
      
   }
   
   return(df_complete)
}

read_dfs()
#> # A tibble: 20 × 3
#>     var1  var2  var3
#>    <dbl> <dbl> <dbl>
#>  1     1     2     7
#>  2     2     3     8
#>  3     3     4     9
#>  4     4     5    10
#>  5     5     6    11
#>  6     1     2     7
#>  7     2     3     8
#>  8     3     4     9
#>  9     4     5    10
#> 10     5     6    11
#> 11     1     2     7
#> 12     2     3     8
#> 13     3     4     9
#> 14     4     5    10
#> 15     5     6    11
#> 16     1     2     7
#> 17     2     3     8
#> 18     3     4     9
#> 19     4     5    10
#> 20     5     6    11

Do you need this?

library(readxl)
library(tidyverse)

setwd("/Users/harshvardhan/Desktop/Dump/codes/sheets/")

read_dfs = function()
{
   # this has to initilised intelligently based on what the variables are
   df_complete = tibble(var1 = numeric(),
                        var2 = numeric(),
                        var3 = numeric())
      
   # listing all files in the directory
   file.list = list.files(pattern='*.xlsx')
   
   # first loop for all files
   for (book in file.list)
   {
      # getting sheets in this workbook
      sheet = excel_sheets(book)
      
      # second loop for all sheets
      for (s in sheet)
      {
         df = read_excel(path = book, sheet = s)
         df_complete = rbind(df_complete, df)
      }
      
   }
   
   return(df_complete)
}

read_dfs()
#> # A tibble: 20 × 3
#>     var1  var2  var3
#>    <dbl> <dbl> <dbl>
#>  1     1     2     7
#>  2     2     3     8
#>  3     3     4     9
#>  4     4     5    10
#>  5     5     6    11
#>  6     1     2     7
#>  7     2     3     8
#>  8     3     4     9
#>  9     4     5    10
#> 10     5     6    11
#> 11     1     2     7
#> 12     2     3     8
#> 13     3     4     9
#> 14     4     5    10
#> 15     5     6    11
#> 16     1     2     7
#> 17     2     3     8
#> 18     3     4     9
#> 19     4     5    10
#> 20     5     6    11

Created on 2022-05-29 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文