2017-12-18 2 views
0

Angenommen, ich habe folgendes:Konvertieren Multi-Layer-Liste von Listen zu einem data.frame?

library(httr) 

foo <- content(GET(paste0("https://api.cryptowat.ch/markets/kraken/ethusd/ohlc?after=1483228800&before=9999999999&periods=86400"))) 

Das Ergebnis foo ist eine Liste von 2 zusätzlichen Listen: result und allowance. Ich muss foo$result (unten) in eine data.frame glätten.

> foo 

$`86400`[[311]] 
$`86400`[[311]][[1]] 
[1] 1510012800 

$`86400`[[311]][[2]] 
[1] 295.1 

$`86400`[[311]][[3]] 
[1] 304.21 

$`86400`[[311]][[4]] 
[1] 291.3 

$`86400`[[311]][[5]] 
[1] 298.5 

$`86400`[[311]][[6]] 
[1] 13792.92 

$`86400`[[311]][[7]] 
[1] 0 


$`86400`[[312]] 
$`86400`[[312]][[1]] 
[1] 1510099200 

$`86400`[[312]][[2]] 
[1] 298.5 

$`86400`[[312]][[3]] 
[1] 303.3 

$`86400`[[312]][[4]] 
[1] 287.28 

$`86400`[[312]][[5]] 
[1] 292.07 

$`86400`[[312]][[6]] 
[1] 32132.04 

$`86400`[[312]][[7]] 
[1] 0 

... 
... 

Die data.frame sollte nrow gleich length(foo$result[[1]]) und ncol gleich 7 haben:

1510012800 295.1 304.21 291.3 298.5 13792.92 0 
1510099200 298.5 303.3 287.28 292.07 32132.04 0 
... 
... 

Ist dies eine schnelle Art und Weise ähnlich der mit lapply oder zu tun? Ich versuchte die ldply Funktion von plyr Paket, so: lapply(foo$result[[1]], FUN = ldply, .fun = data.frame), aber dies gibt immer noch eine Liste von Listen.

Irgendwelche Ideen?

Antwort

2

Hier ist eine Lösung in der Basis R:

df <- as.data.frame(matrix(unlist(foo$result[[1]]), ncol = 7, byrow = TRUE)) 
head(df); 
#   V1  V2  V3  V4  V5  V6 V7 
#1 1483228800 8.14884 8.21477 7.94881 8.06999 42388.83 0 
#2 1483315200 8.10000 8.56100 8.05109 8.19950 53422.02 0 
#3 1483401600 8.19950 8.47500 8.10000 8.39650 39187.21 0 
#4 1483488000 8.36780 10.09948 8.25668 9.56690 234379.75 0 
#5 1483574400 9.63000 11.12499 9.42001 11.02000 210981.02 0 
#6 1483660800 11.09979 11.63786 9.10000 10.31859 223963.44 0 

Erläuterung: unlistfoot$result[[1]], umformatieren resultierenden Vektor als 7 Spalte matrix und wechseln Sie in data.frame.

+0

Danke dafür !! – Ray

1

könnten Sie verwenden map und reduce von purrr, kombiniert mit dplyr::bind_rows

library(httr) 
library(purrr) 
library(dplyr) 

foo <- content(GET(paste0("https://api.cryptowat.ch/markets/kraken/ethusd/ohlc?after=1483228800&before=9999999999&periods=86400"))) 

nm <- paste0("X",1:7) 
foo$result[[1]] %>% 
    map(unlist) %>% 
    map(~setNames(.x, nm)) %>% 
    reduce(bind_rows) 

# A tibble: 354 x 7 
      X1  X2  X3  X4  X5  X6 X7 
     <dbl> <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl> 
1 1483228800 8.14884 8.21477 7.94881 8.06999 42388.83  0 
2 1483315200 8.10000 8.56100 8.05109 8.19950 53422.02  0 
3 1483401600 8.19950 8.47500 8.10000 8.39650 39187.21  0 
4 1483488000 8.36780 10.09948 8.25668 9.56690 234379.75  0 
5 1483574400 9.63000 11.12499 9.42001 11.02000 210981.02  0 
6 1483660800 11.09979 11.63786 9.10000 10.31859 223963.44  0 
7 1483747200 10.19690 10.50000 9.50000 10.15870 223224.03  0 
8 1483833600 10.14620 10.18403 9.54024 9.87100 110811.07  0 
9 1483920000 9.76164 10.47000 9.76164 10.27581 47720.22  0 
10 1484006400 10.30282 10.84499 10.10007 10.40000 29018.72  0 
# ... with 344 more rows 
+0

Dies ist sehr nützlich - wird für die spätere Verwendung speichern! Vielen Dank! – Ray

Verwandte Themen