Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Dealing with deeply nested lists sourced from JSON

$
0
0

everyone!

Have a problem with deeply nested list, sourced from JSON... so, provide an original data → https://dropmefiles.com/luuhx What do I need? A tibble with:

"id" form "data" element

"total_with_discount" from "data" element

"name" from [["data"]][["siteorder_status"]]

"id" form [["data"]][["Cart"]][[.x]][["id"]] (note: .x must relate to indexes in vectors above. also, take a note that some df's in "Cart" elements lists have dimensions 0 x 0)

How did I try to solve this problem

Cause modify_depth doesn't work if we have the same element names in various levels of a list, I tried to extract every list separately.

carts <- response %>% map("data") %>% map("Cart") %>% map(~ .x)

data <- map(data, ~ list_modify(., "Cart" = NULL)) %>%
  map(~ list_modify(., "siteorder_status" = NULL)) %>%
  map(~ list_modify(., "items" = NULL))

siteorder_status <- map(response, "data") %>% map("name") %>% flatten_chr()

And here I've stuck 🤷‍♂️

Is there simpler solution? And how do I deal with empty (dim 0x0) df's in carts (extract/fill id like NAs in requried tibble)?

UPD: Tried to reproduce simple example of my nested list:

<!-- language-all: lang-r -->

    l <- list(list(data = data.frame(id = sample(10000:99999, 5),
                                     shipment_date = sample(seq(as.Date("2019-12-01"), as.Date("2020-01-20"), by= "day"), 5),
                                     stringsAsFactors = F)),
              list(data = data.frame(id = sample(10000:99999, 5),
                                     shipment_date = sample(seq(as.Date("2019-12-01"), as.Date("2020-01-20"), by= "day"), 5),
                                     stringsAsFactors = F)), 
              list(data = data.frame(id = sample(10000:99999, 5),
                                     shipment_date = sample(seq(as.Date("2019-12-01"), as.Date("2020-01-20"), by= "day"), 5),
                                     stringsAsFactors = F)))

    order_status <- c("new", "merged", "shipped", "delivered", "cancelled", "returned")

    for(i in 1:3){
      l[[i]][["data"]][["siteorder_status"]] <- data.frame(id = sample(10:100, 5),
                                         name = sample(order_status, 5, replace = T), stringsAsFactors = F)
    }

    for(i in 1:3){
      for(j in 1:length(l[[i]][["data"]]$id))
        l[[i]][["data"]][["cart"]][[j]] <- data.frame(id = sample(1000:9999, 1),
                                                    date = sample(seq(as.Date("2019-12-01"), as.Date("2020-01-20"), by= "day"), 1),
                                                    fr = runif(1, 100, 10000), stringsAsFactors = F)
    }

    l[[sample(1:3, 1)]][["data"]][["cart"]][[sample(1:5, 1)]] <- data.frame()
    str(l)
    #> List of 3
    #>  $ :List of 1
    #>   ..$ data:'data.frame': 5 obs. of  4 variables:
    #>   .. ..$ id              : int [1:5] 53422 30856 67755 58928 76734
    #>   .. ..$ shipment_date   : Date[1:5], format: "2019-12-13""2019-12-11" ...
    #>   .. ..$ siteorder_status:'data.frame':  5 obs. of  2 variables:
    #>   .. .. ..$ id  : int [1:5] 56 68 64 62 60
    #>   .. .. ..$ name: chr [1:5] "shipped""shipped""merged""new" ...
    #>   .. ..$ cart            :List of 5
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 8676
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-15"
    #>   .. .. .. ..$ fr  : num 8953
    #>   .. .. ..$ :'data.frame':   0 obs. of  0 variables
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 6770
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-19"
    #>   .. .. .. ..$ fr  : num 9606
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 9602
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-08"
    #>   .. .. .. ..$ fr  : num 292
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 6683
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-30"
    #>   .. .. .. ..$ fr  : num 5728
    #>  $ :List of 1
    #>   ..$ data:'data.frame': 5 obs. of  4 variables:
    #>   .. ..$ id              : int [1:5] 54439 69536 22064 36821 24478
    #>   .. ..$ shipment_date   : Date[1:5], format: "2019-12-29""2019-12-06" ...
    #>   .. ..$ siteorder_status:'data.frame':  5 obs. of  2 variables:
    #>   .. .. ..$ id  : int [1:5] 43 30 38 53 76
    #>   .. .. ..$ name: chr [1:5] "merged""new""shipped""new" ...
    #>   .. ..$ cart            :List of 5
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 9079
    #>   .. .. .. ..$ date: Date[1:1], format: "2020-01-02"
    #>   .. .. .. ..$ fr  : num 8395
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 7031
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-20"
    #>   .. .. .. ..$ fr  : num 994
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 3792
    #>   .. .. .. ..$ date: Date[1:1], format: "2020-01-07"
    #>   .. .. .. ..$ fr  : num 2799
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 6807
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-10"
    #>   .. .. .. ..$ fr  : num 8252
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 3717
    #>   .. .. .. ..$ date: Date[1:1], format: "2020-01-11"
    #>   .. .. .. ..$ fr  : num 7503
    #>  $ :List of 1
    #>   ..$ data:'data.frame': 5 obs. of  4 variables:
    #>   .. ..$ id              : int [1:5] 17658 63868 43326 90201 55157
    #>   .. ..$ shipment_date   : Date[1:5], format: "2019-12-25""2019-12-27" ...
    #>   .. ..$ siteorder_status:'data.frame':  5 obs. of  2 variables:
    #>   .. .. ..$ id  : int [1:5] 13 48 29 20 47
    #>   .. .. ..$ name: chr [1:5] "returned""merged""returned""merged" ...
    #>   .. ..$ cart            :List of 5
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 7101
    #>   .. .. .. ..$ date: Date[1:1], format: "2020-01-20"
    #>   .. .. .. ..$ fr  : num 9917
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 1730
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-01"
    #>   .. .. .. ..$ fr  : num 4377
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 5641
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-11"
    #>   .. .. .. ..$ fr  : num 7010
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 7754
    #>   .. .. .. ..$ date: Date[1:1], format: "2019-12-23"
    #>   .. .. .. ..$ fr  : num 2551
    #>   .. .. ..$ :'data.frame':   1 obs. of  3 variables:
    #>   .. .. .. ..$ id  : int 5568
    #>   .. .. .. ..$ date: Date[1:1], format: "2020-01-02"
    #>   .. .. .. ..$ fr  : num 3127
    l
    #> [[1]]
    #> [[1]]$data
    #>      id shipment_date siteorder_status.id siteorder_status.name
    #> 1 53422    2019-12-13                  56               shipped
    #> 2 30856    2019-12-11                  68               shipped
    #> 3 67755    2020-01-03                  64                merged
    #> 4 58928    2020-01-13                  62                   new
    #> 5 76734    2020-01-06                  60                   new
    #>                              cart
    #> 1   8676.000, 18245.000, 8952.782
    #> 2                            NULL
    #> 3   6770.000, 18249.000, 9606.059
    #> 4 9602.0000, 18238.0000, 291.5286
    #> 5   6683.000, 18260.000, 5728.442
    #> 
    #> 
    #> [[2]]
    #> [[2]]$data
    #>      id shipment_date siteorder_status.id siteorder_status.name
    #> 1 54439    2019-12-29                  43                merged
    #> 2 69536    2019-12-06                  30                   new
    #> 3 22064    2020-01-14                  38               shipped
    #> 4 36821    2019-12-17                  53                   new
    #> 5 24478    2019-12-09                  76               shipped
    #>                              cart
    #> 1   9079.000, 18263.000, 8394.667
    #> 2 7031.0000, 18250.0000, 993.6618
    #> 3   3792.000, 18268.000, 2798.709
    #> 4   6807.000, 18240.000, 8252.129
    #> 5   3717.000, 18272.000, 7502.621
    #> 
    #> 
    #> [[3]]
    #> [[3]]$data
    #>      id shipment_date siteorder_status.id siteorder_status.name
    #> 1 17658    2019-12-25                  13              returned
    #> 2 63868    2019-12-27                  48                merged
    #> 3 43326    2019-12-12                  29              returned
    #> 4 90201    2020-01-10                  20                merged
    #> 5 55157    2020-01-05                  47                merged
    #>                            cart
    #> 1 7101.000, 18281.000, 9916.571
    #> 2 1730.000, 18231.000, 4376.873
    #> 3 5641.000, 18241.000, 7010.191
    #> 4 7754.000, 18253.000, 2551.029
    #> 5 5568.000, 18263.000, 3126.706

<sup>Created on 2020-01-20 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>