0

i try to convert the okex-ticker to a dataframe, using dplyr library, which was very successfull on other marketplaces.

Here´s the entire Code:

library(dplyr)
library(httr)
GET(
  url = "https://www.okex.com/v2/spot/markets/tickers"
) -> okexRes
x5=content(okexRes)
dfokex <- bind_rows(x5$data)

I think there´s something missing in brackets of the last line, like (x5$data$???), but in the environment of RStudio it looks like this

x5        Large list
 code : int 0
 data :List of 407
 ..$ :List of 24
 .. .. $ brokerId: int 0
 .. .. $ buy: chr "9314.4"
 .. .. $ change: ...

Whats the name(?) of the second list here? Is there a function for showing the names? Thanks in advance :)

camille
  • 16,432
  • 18
  • 38
  • 60
Tribic
  • 108
  • 1
  • 9
  • Possible duplicate of https://stackoverflow.com/questions/45452015/how-to-convert-list-of-list-into-a-tibble-dataframe – GenesRus Nov 04 '19 at 23:48
  • There's no name of that list; data is an unnamed list of named lists. I think you're getting at the same problem answered in the post above. – GenesRus Nov 04 '19 at 23:52

1 Answers1

1

One problem that I see here is that the response from that API is containing an empty object under the json-key "name". This looks like {[...], "name":{}, [...]} in the ticker.
R is converting that into an empty list - which will then cause an error when you try to coerce the entries of data into a dataframe.

What will easily help you with your problem is if you add one step where you flatten each entry of data, which will convert the list into a dataframe ready for row-binding.

Try this:

library(dplyr)
library(purrr)

dfokex <- x5$data %>%
  lapply(flatten_dfr) %>%
  bind_rows()
alex_jwb90
  • 1,663
  • 1
  • 11
  • 20
  • Thanks a lot! Working great after adding one line between lapply and bind rows, cause of an error: Column `currencyId` can't be converted from character to integer. To get it work, add: lapply(., mutate_if, is.integer, as.character) %>% – Tribic Nov 05 '19 at 16:31
  • Oh sorry for that then. I only tested it on the first bunch of entries so that didn't pop up on my end. Good you figured it out :) – alex_jwb90 Nov 06 '19 at 03:56