2

it is a basic command, but i cannot get around it.

how can one extract/filter a data.frame inside a list which is inside a data.frame...?

library(jsonlite)

url <- "https://www.ine.pt/ine/json_indicador/pindica.jsp?op=2&varcd=0000611&lang=PT"

data <- fromJSON(url) 

data results in 1 observation with 8 variables.

how to access the data.frame of 7329 observations?

data$Dados$2013$data.frame

Any help is deeply appreciated :) Thanks

jay.sf
  • 60,139
  • 8
  • 53
  • 110

2 Answers2

2

For extracting elements from deeply nested lists, purrr::pluck is very helpful:

library(purrr)
library(dplyr)

data %>%
    pluck(7, 1, 1) %>%
    tibble()

#OR

data %>%
    pluck('Dados', '2013', 1) %>%
    tibble()

We can also use the iteractive dolar sign until we get the final unnamed length-1 list, then extract that last element with the double brackets ([[]]) or with purrr::list_flatten() (this requires purrr 1.0.0)

data$Dados$`2013`[[1]]

#OR#

data$Dados$`2013` |> list_flatten()

output

# A tibble: 7,329 × 7
   geocod geodsg                     dim_3 dim_3_t dim_4 dim_4_t valor  
   <chr>  <chr>                      <chr> <chr>   <chr> <chr>   <chr>  
 1 1      Continente                 1     H       T     Total   4714328
 2 200    Região Autónoma dos Açores 1     H       T     Total   121646 
 3 2      Região Autónoma dos Açores 1     H       T     Total   121646 
 4 20     Região Autónoma dos Açores 1     H       T     Total   121646 
 5 3      Região Autónoma da Madeira 1     H       T     Total   122046 
 6 300    Região Autónoma da Madeira 1     H       T     Total   122046 
 7 PT     Portugal                   1     H       T     Total   4958020
 8 30     Região Autónoma da Madeira 1     H       T     Total   122046 
 9 113    Ave                        1     H       T     Total   243784 
10 111    Minho-Lima                 1     H       T     Total   111804 
# … with 7,319 more rows
# ℹ Use `print(n = ...)` to see more rows
GuedesBF
  • 8,409
  • 5
  • 19
  • 37
1

got it...

library(tibble)

df <- as_tibble(data[[7]][[1]][[1]])