httr CSV内容读取为整数而不是double

时间:2017-04-17 20:49:58

标签: r csv httr zuora

我正在尝试使用来自在线资源的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.

感谢任何帮助。

1 个答案:

答案 0 :(得分:3)

httr::content功能在您通过readr::read_csv时使用type = "text/csv"。您可以将参数传递到read_csv中的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")