简体   繁体   English

如何使用 purrr:map() 从多个 excel 文件中读取工作表?

[英]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.

相关问题 如何使用 R purrr 组合数百个 Excel 文件/工作表 - How to use R purrr to combine hundreds of Excel files/sheets 如何使用 purrr 中的 map_* function 读取多个文件以保存和计算每个文件中的变量数? - How to read multiple files to save and count number of variables in each using a map_* function from purrr? 如何同时读取Excel工作表并使用purrr / dplyr突变新列? - How to simultaneously read in excel sheets and mutate a new column with purrr/dplyr? 使用 purrr 和 readxl 从多个 excel 文件中读取一个工作表并添加字段 - Read one worksheet from multiple excel files using purrr and readxl and add field 如何将 purrr map 与 dataframe 中的列一起使用? - How to use purrr map with columns from a dataframe? 如何使用来自Shiny to temp目录中的downloadHandler()内的官员以purrr :: map()样式编写多个docx文件? - How to write multiple docx files in purrr::map() style with officer from within downloadHandler() in Shiny to temp directory? R:使用正则表达式从多个 Excel 文件中导入特定工作表 - R: Use Regex to Import Specific Sheets from Multiple Excel Files 来自purrr的map中的if条件 - Multiple if conditions in map from purrr 如何使用 purrr 中的 map 和 dplyr::mutate 根据列对创建多个新列 - How to use map from purrr with dplyr::mutate to create multiple new columns based on column pairs 如何在R中使用purrr :: map() - How to use the purrr::map() in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM