r - 处理来自 JSON 的深度嵌套列表
问题描述
每个人!
来自 JSON 的深度嵌套列表有问题......所以,提供原始数据 → https://dropmefiles.com/luuhx 我需要什么?一个小标题:
“id”表单“数据”元素
“数据”元素中的“total_with_discount”
来自 [["data"]][["siteorder_status"]] 的“名称”
"id" 形式 [["data"]][["Cart"]][[.x]][["id"]] (注意:.x 必须与上述向量中的索引相关。另外,请注意“购物车”元素列表中的一些 df 的尺寸为 0 x 0)
我是如何尝试解决这个问题的
如果我们在列表的各个级别中具有相同的元素名称,因为 modify_depth 不起作用,我尝试分别提取每个列表。
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()
在这里我卡住了♂️</p>
有更简单的解决方案吗?以及如何处理购物车中的空(dim 0x0)df(提取/填充 id,如 requried tibble 中的 NA)?
UPD:试图重现我的嵌套列表的简单示例:
<!-- 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>
解决方案
推荐阅读
- c# - Roslyn-查找未使用的变量
- ios - 在 ios 中关闭 sqlite db 时出错
- java - Apache POI WorkbookFatory.create() 在 Android Studio 中导致 NullPointerException
- javascript - 搜索和过滤手风琴的数据在 vuejs 中不起作用
- php - OCI_BIND_BY_NAME 中的异常行为
- mysql - 我可以在一个 MySQL 数据库中使用 2 个 WordPress 数据库吗
- c++ - c ++输出一次太多次
- python - 在安装了 ubuntu 的 python 3.7 上安装 pytorch 会报错
- python - Django 单元测试用例
- akka - 与 Akka 相比,Akka 流中的并行性