stevecondylios / priceR

Economics and Pricing in R
https://stevecondylios.github.io/priceR/
Other
58 stars 7 forks source link

Cannot handle missing values #56

Open ShannonS-3791 opened 10 months ago

ShannonS-3791 commented 10 months ago

I am running the historical_exchange_rates function for multiple currencies; back to 2010. While the function works fine most of the time, I occasionally go this I get this error:

Error in pmap(): ℹ In index: 1. Caused by error in purrr::map_dbl(): ℹ In index: 1. ℹ With name: 2010-01-01. Caused by error in .x[[index]]: ! subscript out of bounds Backtrace: 1. priceR::historical_exchange_rates(...) 13. purrr::map_dbl(...) 14. purrr:::map_("double", .x, .f, ..., .progress = .progress) 18. priceR (local) .f(.x[[i]], ...)

I investigated this using the code from git, and I believe the issue occurs when running the 'get_values' function when there is no data available for a certain period. The list created via fromJSON has a line with the date, but no additional piece with the data to apply the purrr::map_dbl to.

Thank you for your attention to this matter.

ShannonS-3791 commented 10 months ago

I came up with a temporary fix on my end; and hoping my work can help. I'm not the best with apply/map, so I'm confident this (for loop) is not the most efficient code, but it gets to the root of the problem. I inserted the following code immediately after the dat <- endpoint %>% fromJSON step

dat_fix <- dat[[8]]

  rows_to_delete <- c()

  for (l in 1:length(dat_fix)) {
    if (length(dat_fix[[l]]) == 0) {
      rows_to_delete <- append(rows_to_delete,l)
    }
  }

  dat2 <- if(length(rows_to_delete)>0){
    dat_fix[-rows_to_delete]
  } else {dat_fix}

then in subsequent code, the dat[[8]] or response[[8]] had to be replaced with just dat2 or response