使用read_excel获取具有不同列类型集的多张图纸
我想读取包含多个工作表的excel文件,并为每个工作表指定列类型,以下是我的代码:使用read_excel获取具有不同列类型集的多张图纸,r,purrr,readxl,R,Purrr,Readxl,我想读取包含多个工作表的excel文件,并为每个工作表指定列类型,以下是我的代码: data_file <- "./input/data.xlsx" sheet_names <- c("Sales", "Customs", "Departments") col_type_list <- list( Sales = c(rep("text", 3), rep("numeric", 2)), Customs = rep("text", 5), Departments
data_file <- "./input/data.xlsx"
sheet_names <- c("Sales", "Customs", "Departments")
col_type_list <- list(
Sales = c(rep("text", 3), rep("numeric", 2)),
Customs = rep("text", 5),
Departments = rep("text", 4)
)
DList <- purrr::map2(.x = sheet_names, .y = col_type_list,
.f = read_xlsx(
path = data_file,
sheet = .x,
col_types = .y
)
)
如何在代码中输入正确的.x和.y?您做得很好,但在调用read\u xlsx之前缺少波浪线 在这里添加了波浪线(~),运行平稳:)
data\u文件我想它应该是map2(.x=sheet\u name,.y=col\u type\u list,.f=~read\u xlsx(
。之后的一切都可以保持不变。关键是使用~
告诉map2
接下来的不仅仅是一个函数名(比如read\u xlsx
),但应转换为函数调用的公式。请参阅map2
帮助中的.f
部分(另外,.x=
、.y=
和.f=
也不是必需的,但包含它们并没有坏处。)
Error in standardise_sheet(sheet, range, sheets_fun(path)) :
object '.x' not found
data_file <- "./input/data.xlsx"
sheet_names <- c("Sales", "Customs", "Departments")
col_type_list <- list(
Sales = c(rep("text", 3), rep("numeric", 2)),
Customs = rep("text", 5),
Departments = rep("text", 4)
)
DList <- purrr::map2(.x = sheet_names, .y = col_type_list,
.f = ~read_xlsx(
path = data_file,
sheet = .x,
col_types = .y
)
)