使用read_excel获取具有不同col_types设置的多个工作表

时间:2018-02-10 23:51:11

标签: r purrr readxl

我想阅读包含多个工作表的excel文件,并为每个工作表指定col_types,以下是我的代码:

MATCH (p:Product), (s:Student), (b:Boy), (a:Attribute)
RETURN count(distinct(p)), count(distinct(s)), count(distinct(b)), count(distinct(a))

执行上面的代码并收到以下错误消息:

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放入代码?

1 个答案:

答案 0 :(得分:0)

你做得对,但在调用read_xlsx之前你错过了波浪号。

这里添加了波浪号(〜)并且运行顺畅:)

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
                      )
)