如何将多个.xls文件(每个包含多个工作表)导入R版本3.5.3?

问题描述 投票:0回答:1

我有27个xls文件要导入到R中,并且每个文件在工作簿中都有多个工作表。我试图一次上传所有文件,只有每个文件中的第一张纸被导入到自己的数据框中(此时不需要导入其他纸张)。

我已经看到一些创建文件列表然后使用包readxl,但我使用的是最新版本的R(3.5.3),据我所知,它不兼容。

我希望最终得到27个单独的数据帧,然后我可以添加一个列来标识特定的数据帧,因此它们都可以组合成一个数据帧来使用。

r import xls
1个回答
0
投票

文件列表和readxl工作得很好。我创建了3个excel文件

Mappe1x - Plate1 - A1:Ac - 1,2,h,h

Mappe2.xls - Plateau1 - A1:啊 - h,2,h,h

Mappex - Plateau1 - A1:Ah - h,2,h,h

如果您使用该代码

library(readxl)
library(tidyverse)

# define the names of the excel files 
excelNames <- paste0('Mappe', 1:3, '.xlsx')

lapply(1:length(excelNames), function(i) {

# get current ID and rid of the file extension   
currentID <- str_split(excelNames[i], '.xlsx', simplify = TRUE)[1]

# read excel file and add column with id
read_excel(
 excelNames[i],
 sheet = 'Tabelle1',
 col_names = FALSE,
 range = cell_limits(c(1, 1), c(4, 1))) %>%
 mutate(ID = currentID)
}) %>% 

# bind all results into one dataframe
bind_rows()

你应该得到

# A tibble: 12 x 2
    ...1 ID    
   <dbl> <chr> 
 1     1 Mappe1
 2     2 Mappe1
 3     3 Mappe1
 4     4 Mappe1
 5     4 Mappe2
 6     2 Mappe2
 7     3 Mappe2
 8     4 Mappe2
 9     4 Mappe3
10     2 Mappe3
11     3 Mappe3
12     4 Mappe3
© www.soinside.com 2019 - 2024. All rights reserved.