/ / Estrazione dei dati dalla lista in R - json, r, lista

Estrazione dei dati dalla lista in R - json, r, lista

library(RCurl)
library(rjson)
json <- getURL("https://extraction.import.io/query/runtime/17d882b5-c118-4f27-8ce1-90085ec0b116?_apikey=d5a8a01e20174e95887dc0f385e4e3f6d7ef5ca1428d5a029f2aa352509948ade8e5d7fb0dc941f4769a32b541ca6b38a7cd6578dfd81b357fbc4f2e008f5154f1dbfcff31878798fa887b70b1ff59dd&url=http%3A%2F%2Fwww.numbeo.com%2Fcost-of-living%2Fcompare_cities.jsp%3Fcountry1%3DSingapore%26country2%3DAustralia%26city1%3DSingapore%26city2%3DMelbourne")
obj <- fromJSON(json)

Mi piacerebbe ottenere i dati in colonne di dati, ma molti passaggi nell'elenco sono "senza nome". Qualche idea su come organizzare i dati?

risposte:

1 per risposta № 1

Dai un'occhiata a questa differenza e fammi sapere cosa ne pensi. Questo è come appare il tuo oggetto:

library(RCurl)
library(rjson)
json <- getURL("https://extraction.import.io/query/runtime/17d882b5-c118-4f27-8ce1-90085ec0b116?_apikey=d5a8a01e20174e95887dc0f385e4e3f6d7ef5ca1428d5a029f2aa352509948ade8e5d7fb0dc941f4769a32b541ca6b38a7cd6578dfd81b357fbc4f2e008f5154f1dbfcff31878798fa887b70b1ff59dd&url=http%3A%2F%2Fwww.numbeo.com%2Fcost-of-living%2Fcompare_cities.jsp%3Fcountry1%3DSingapore%26country2%3DAustralia%26city1%3DSingapore%26city2%3DMelbourne")
obj <- rjson::fromJSON(json)
str(obj)

List of 2
$ extractorData:List of 3
..$ url       : chr "http://www.numbeo.com/cost-of-living/compare_cities.jsp?country1=Singapore&country2=Australia&city1=Singapore&city2=Melbourne"
..$ resourceId: chr "b1250747011ee774e7c881617c86a5a9"
..$ data      :List of 1
.. ..$ :List of 1
.. .. ..$ group:List of 52
.. .. .. ..$ :List of 6
.. .. .. .. ..$ COL VALUE        :List of 1
.. .. .. .. .. ..$ :List of 1
.. .. .. .. .. .. ..$ text: chr "Meal, Inexpensive Restaurant"

Effettivamente molte Liste tra le quali non ne hai bisogno Ora prova il jsonlite pacchi fromJSON funzione:

library(jsonlite)
obj2<- jsonlite::fromJSON(json)

List of 2
$ extractorData:List of 3
..$ url       : chr "http://www.numbeo.com/cost-of-living/compare_cities.jsp?country1=Singapore&country2=Australia&city1=Singapore&city2=Melbourne"
..$ resourceId: chr "b1250747011ee774e7c881617c86a5a9"
..$ data      :"data.frame":  1 obs. of  1 variable:
.. ..$ group:List of 1
.. .. ..$ :"data.frame":  52 obs. of  6 variables:
.. .. .. ..$ COL VALUE        :List of 52
.. .. .. .. ..$ :"data.frame":    1 obs. of  1 variable:
.. .. .. .. .. ..$ text: chr "Meal, Inexpensive Restaurant"
.. .. .. .. ..$ :"data.frame":    1 obs. of  1 variable:
.. .. .. .. .. ..$ text: chr "Meal for 2 People, Mid-range Restaurant, Three-course"
.. .. .. .. ..$ :"data.frame":    1 obs. of  1 variable:

Tuttavia, questo JSON non è bello, avremo bisogno di sistemarlo. Suppongo che vogliate quella cornice dati lì dentro. Quindi inizia con

df <- obj2$extractorData$data$group[[1]]

e c'è il tuo frame di dati. Problema però: ogni singola cella è in una lista qui. Includendo valori NULL, e non puoi semplicemente non elencarli, questi scompariranno e le colonne in cui erano diventeranno più brevi ...

Modifica: Ecco come gestire le colonne con list(NULL) valori.

df[sapply(df[,2],is.null),2] <- NA
df[sapply(df[,3],is.null),3] <- NA
df[sapply(df[,4],is.null),4] <- NA
df[sapply(df[,5],is.null),5] <- NA
df2 <- sapply(df, unlist) %>% as.data.frame

Può essere scritto in modo più elegante, ma questo ti aiuterà e sarà comprensibile.