我有27个xls文件要导入到R中,每个文件在工作簿中都有多个工作表。我正在尝试一次上传所有文件,每个文件中只有第一个工作表被导入到自己的数据框中(其他工作表此时不需要导入)。
我看到一些人创建了一个文件列表,然后使用readxl包,但是我使用的是最新版本的R (3.5.3),据我所知它不兼容。
我希望最终得到27个单独的数据帧,然后我可以添加一个列来标识特定的数据帧,这样它们都可以组合到一个数据帧中进行处理。
发布于 2019-04-19 17:27:02
文件列表和readxl运行得很好。我已经创建了3个excel文件
Mapp1.xlsx- Tabelle1 - A1:A4 - 1,2,3,4
Mapp2.xlsx- Tabelle1 - A1:A4 - 4,2,3,4
图3.xlsx- Tabelle1 - A1:A4 - 4,2,3,4
如果您使用以下代码
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 Mappe3https://stackoverflow.com/questions/55755513
复制相似问题