【问题标题】:Error when mapping function / Works fine when I run it manually piece by piece; Why?映射功能时出错/当我逐个手动运行它时工作正常;为什么?
【发布时间】:2020-04-01 15:34:27
【问题描述】:

对于这个问题中代码的大小,我深表歉意,但由于我无法确定哪里出了问题,我认为最好提供尽可能接近原始代码的代码。

我有以下环境,有两个功能:

第一个函数应该将一系列路径作为输入来读取 .csv 文件(EMPRESA_reads),绑定它们并导出:

library(dplyr)
library(purrr)
library(glue)
library(data.table)

read_compile = function(EMPRESA_reads,Export_path){

  cat("Reading EMPRESA files... \n")
  EMPRESA_Imported = map(EMPRESA_reads,fread,sep = ",")

  cat("Binding EMPRESA rows... \n")
  EMPRESA_Imported = EMPRESA_Imported%>%
    lapply(., mutate_if, is.integer, as.character)%>%
    bind_rows()

  cat("Exporting file... \n")
  setwd(Export_path)
  fwrite(EMPRESA_Imported,file = "EMPRESA_compiled.csv",sep = ";")

  cat("Done! \n")
}

第二个函数应该将标识符字符串(RFB_date)作为输入,在第一个函数中生成正确的输入路径,然后调用它:

Merge_by_date = function(RFB_date){

  cat(glue("Starting RFB_date {RFB_date}"))
  cat("\n")

  EMPRESA_path = glue("path/RFB_Empresas_{RFB_date}")
  Export_path = glue("path/RFB_Socios_{RFB_date}/With_EMPRESA_info")

  # Creating list of files to be read in each folder
  # Check the file name pattern and adjust accordingly

  EMPRESA_files = list.files(path = EMPRESA_path, pattern = "^empresas")
  EMPRESA_reads = glue("{EMPRESA_path}/{EMPRESA_files}")

  read_compile(EMPRESA_reads,Export_path)
}

我想为多个日期运行第二个函数,所以我映射它们:

RFB_dates = c("201902",
              "201906",
              "201911",
              "202002")

map(RFB_dates,Merge_by_date)

然而,在第一次运行(RFB_dates[1])时,我得到了这个输出:

Starting RFB_date 201902
Reading EMPRESA files... 
Binding EMPRESA rows... 

 Error in bind_rows_(x, .id) : 
  STRING_ELT() can only be applied to a 'character vector', not a 'char' 

这表明在此运行中绑定EMPRESA_Imported 列表的元素时出现问题,但之前没有。

问题是:当手动运行该函数一次(而不是映射它)时,它不会抛出此错误,而是按照应有的方式执行bind_rows()

我还没有弄清楚为什么会发生这种情况,感谢任何帮助!

关于我正在阅读的 .csv 文件:它们非常大(总共约 15gb)但没有任何异常:只是包含字符串和数字数据的列。

谢谢!

【问题讨论】:

  • 你看过这个吗?SOF?s80974671,好吧,不是完整的 url,而是查找 SOF? 34184851。基本上建议,由于持续的摔跤、调试、写作等,只需关闭您的会话并重新开始。但是没有数据,不知道您的列中是否有因素等,也许还有这个SOF?s49062888
  • 你应该尽量让你的例子最小化,不确定问题是来自胶水、咕噜声还是 data.table。 bind_rows 似乎是问题所在,因此您应该去掉所有这些循环并专注于引发错误的 bind_rows 情况。
  • @jangorecki,问题是:如果我做的事情少于显示的内容,则没有错误。即使在没有地图的情况下运行(仅运行函数)也可以使用地图首先执行的确切数据。我已经将所有层都放到了这个例子中,因为它们都存在并且我无法将它们排除为错误的可能来源编辑:无论如何,这可能是我决定如何在这里提问的问题......我会尽量坚持更具体的问题

标签: r dplyr data.table purrr


【解决方案1】:

试试:

read_compile <- function(EMPRESA_reads,Export_path){

cat("Reading EMPRESA files... \n")
EMPRESA_Imported = map(EMPRESA_reads, ~ fread(.x, sep = ","))

cat("Binding EMPRESA rows... \n")
EMPRESA_Imported <- EMPRESA_Imported %>%
map(~ mutate_if(.x, is.integer(), as.character())) %>%
bind_rows()

cat("Exporting file... \n")
setwd(Export_path)
fwrite(EMPRESA_Imported,file = "EMPRESA_compiled.csv",sep = ";")

 cat("Done! \n")
}

【讨论】:

  • 好吧,得到一个不同的错误:` is.integer() 中的错误:0 个参数传递给 'is.integer' 这需要 1 个 `
猜你喜欢
  • 1970-01-01
  • 2021-02-01
  • 2021-02-07
  • 2020-06-26
  • 2020-09-25
  • 1970-01-01
  • 1970-01-01
  • 2014-02-03
  • 2020-10-09
相关资源
最近更新 更多