Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to import a csv using httr from an online source, everything is fine except for it's reading a column as integer when it should be a double resulting in those values to show up as NA

I'm using and getting the below issue.

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.

Any help is appreciated, thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
147 views
Welcome To Ask or Share your Answers For Others

1 Answer

The httr::content function uses readr::read_csv when you pass type = "text/csv". You can pass parameters through to read_csv within content and luckily read_csv allows you to define the column types of the csv you're importing.

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

Notice Invoice.Balance = col_double().

See vignette("column-types", package = "readr") for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...