[英]How to use purrr:map() to read sheets from multiple excel files?
I have a tibble with a column for file paths and a column for the name of the sheets (paths removed for security):我有一个带有文件路径列和工作表名称列的小标题(为安全起见,删除了路径):
x <- structure(list(file = c("2003_2005_DataDictionary.xlsx", "2003_2005_DataDictionary.xlsx",
"2003_2005_DataDictionary.xlsx", "2003_2005_DataDictionary.xlsx",
"2003_2005_DataDictionary.xlsx", "2003_2005_DataDictionary.xlsx",
"1996_2002_DataDictionary.xlsx", "1996_2002_DataDictionary.xlsx",
"1996_2002_DataDictionary.xlsx", "1996_2002_DataDictionary.xlsx",
"1996_2002_DataDictionary.xlsx", "1996_2002_DataDictionary.xlsx",
"1996_2002_DataDictionary.xlsx", "1996_2002_DataDictionary.xlsx",
"1996_2002_DataDictionary.xlsx", "1996_2002_DataDictionary.xlsx",
"1996_2002_DataDictionary.xlsx", "1996_2002_DataDictionary.xlsx",
"1996_2002_DataDictionary.xlsx", "1996_2002_DataDictionary.xlsx"
), sheet = c("vartable05", "vartable04", "vartable03", "valuesets05",
"valuesets04", "valuesets03", "vartable02", "vartable01", "vartable00",
"vartable99", "vartable98", "vartable97", "vartable96", "valuesets02",
"valuesets01", "valuesets00", "valuesets99", "valuesets98", "valuesets97",
"valuesets96")), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA,
-20L))
> x
# A tibble: 20 x 2
file sheet
<chr> <chr>
1 2003_2005_DataDictionary.xlsx vartable05
2 2003_2005_DataDictionary.xlsx vartable04
3 2003_2005_DataDictionary.xlsx vartable03
4 2003_2005_DataDictionary.xlsx valuesets05
5 2003_2005_DataDictionary.xlsx valuesets04
6 2003_2005_DataDictionary.xlsx valuesets03
7 1996_2002_DataDictionary.xlsx vartable02
8 1996_2002_DataDictionary.xlsx vartable01
9 1996_2002_DataDictionary.xlsx vartable00
10 1996_2002_DataDictionary.xlsx vartable99
11 1996_2002_DataDictionary.xlsx vartable98
12 1996_2002_DataDictionary.xlsx vartable97
13 1996_2002_DataDictionary.xlsx vartable96
14 1996_2002_DataDictionary.xlsx valuesets02
15 1996_2002_DataDictionary.xlsx valuesets01
16 1996_2002_DataDictionary.xlsx valuesets00
17 1996_2002_DataDictionary.xlsx valuesets99
18 1996_2002_DataDictionary.xlsx valuesets98
19 1996_2002_DataDictionary.xlsx valuesets97
20 1996_2002_DataDictionary.xlsx valuesets96
I need to read each sheet using read_excel()
and combine to a single data frame, however the following code doesn't work:我需要使用
read_excel()
读取每个工作表并将其合并为一个数据框,但是以下代码不起作用:
x %>%
map_dfr(~read_excel(path = .$file, sheet = .$sheet))
#Error in .$file : $ operator is invalid for atomic vectors
```
What am I doing wrong? I've tried imap as well with no luck.
We can either use map2
for corresponding elements of each column我们可以将
map2
用于每列的相应元素
library(purrr)
map2(x$file, x$sheet, ~ read_excel(path = .x, sheet = .y)
Or with pmap
或者用
pmap
pmap(x, ~ read_excel(path = ..1, sheet = ..2))
Or if we rename the columns,或者如果我们重命名列,
x %>%
rename(path = file) %>%
pmap(read_excel)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.