-1

我正在尝试使用来自在线资源的 httr 导入 csv,一切都很好,除了当它应该是双精度时它以整数形式读取列,导致这些值显示为 NA

我正在使用并遇到以下问题。

getdata <- GET(paste("https://rest.zuora.com/v1/files/",r$FileId, sep = ''), auth) invoice <- content(getdata, type = "text/csv")

  Parsed with column specification:
cols(
  Account.external_id__c = col_integer(),
  Invoice.Amount = col_double(),
  Invoice.Balance = col_integer(),
  Invoice.CreatedDate = col_datetime(format = "")
)
Warning: 171 parsing failures.
 row             col               expected actual
2475 Invoice.Balance no trailing characters    .4 
2726 Invoice.Balance no trailing characters    .71
3197 Invoice.Balance no trailing characters    .3 
3287 Invoice.Balance no trailing characters    .5 
3350 Invoice.Balance no trailing characters    .1 
.... ............... ...................... ......
See problems(...) for more details.

任何帮助表示赞赏,谢谢。

4

1 回答 1

6

httr::content函数readr::read_csv在您传递时使用type = "text/csv"。您可以将参数传递给read_csvinside content,幸运的是read_csv,您可以定义要导入的 csv 的列类型。

invoice <- content(getdata, type = "text/csv", col_types = cols(
  Account.external_id__c = col_integer(),
  Invoice.Amount = col_double(),
  Invoice.Balance = col_double(),
  Invoice.CreatedDate = col_datetime(format = "")
))

注意Invoice.Balance = col_double()

有关vignette("column-types", package = "readr")更多信息,请参阅。

于 2017-04-18T15:47:33.880 回答