[英]R: flatten inner part of a list of lists while keeping its structure
我正在進行一項模擬研究,我的結果存儲在嵌套列表結構中。 列表的第一級代表 model 生成的不同超參數。 第二個級別是相同model的復制次數(改變種子)。
在下面的示例中,我列出了由兩個超參數(hyperpar1 和 hyperpar2)控制的 model 的 output,其中兩者都可以采用 2 個不同的值,從而導致生成的 Z20F35E630DAF49DFA8 的 4 種不同組合此外,4 種可能的組合中的每一種都運行了兩次(不同的種子),產生了 8 種可能的組合(如下所示str(res, max = 2)
)。 最后,從模型的每次可能迭代中恢復了兩個性能指標(metric1 和 metric2),以及 model beta = list(b1 = value, b2 = value)
的兩個參數的值。
我的問題與最后一部分有關。 我想展平列表beta
並將列表中的每個組件作為包含它的上列表的組件,但保持開頭描述的整個結構不變。
下面是一個例子:
res <-list(
list(list(modeltype = "tree", time_iter = structure(0.7099, class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 0.5, metric1 = 0.4847, metric2 = 0.2576, beta = list(b1 = 0.575, b2 =0.745)),
list(modeltype = "tree", time_iter = structure(0.058 , class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 0.5, metric1 = 0.4013, metric2 = 0.2569, beta = list(b1 = 0.535, b2 =0.775))),
list(list(modeltype = "tree", time_iter = structure(0.046 , class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 0.5, metric1 = 0.4755, metric2 = 0.2988, beta = list(b1 = 0.541, b2 =0.702) ),
list(modeltype = "tree", time_iter = structure(0.0474, class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 0.5, metric1 = 0.2413, metric2 = 0.2147, beta = list(b1 = 0.545, b2 =0.793) )),
list(list(modeltype = "tree", time_iter = structure(0.0502, class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 1 , metric1 = 0.7131, metric2 = 0.5024, beta = list(b1 = 0.500, b2 =0.722) ),
list(modeltype = "tree", time_iter = structure(2.9419, class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.5, hyperpar2 = 1 , metric1 = 0.4254, metric2 = 0.2824, beta = list(b1 = 0.555, b2 =0.712) )),
list(list(modeltype = "tree", time_iter = structure(0.041 , class = "difftime", units = "secs"),seed = 1, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 1 , metric1 = 0.6709, metric2 = 0.4092, beta = list(b1 = 0.578, b2 =0.701) ),
list(modeltype = "tree", time_iter = structure(0.0396, class = "difftime", units = "secs"),seed = 2, nobs = 75, hyperpar1 = 0.8, hyperpar2 = 1 , metric1 = 0.4585, metric2 = 0.4115, beta = list(b1 = 0.501, b2 =0.777) )))
str(res[[1]][[1]], max = 3)
# List of 9
# $ modeltype: chr "tree"
# $ time_iter: 'difftime' num 0.7099
# ..- attr(*, "units")= chr "secs"
# $ seed : num 1
# $ nobs : num 75
# $ hyperpar1: num 0.5
# $ hyperpar2: num 0.5
# $ metric1 : num 0.485
# $ metric2 : num 0.258
# $ beta :List of 2 #### <----- ( here is what I want to flatten/unlist )
# ..$ b1: num 0.575
# ..$ b2: num 0.745
str(res[[1]][[1]], max = 3)
# List of 9
# $ modeltype: chr "tree"
# $ time_iter: 'difftime' num 0.7099
# ..- attr(*, "units")= chr "secs"
# $ seed : num 1
# $ nobs : num 75
# $ hyperpar1: num 0.5
# $ hyperpar2: num 0.5
# $ metric1 : num 0.485
# $ metric2 : num 0.258
# $ b1: num 0.575 #### <----- ( here is new because )
# $ b2: num 0.745 #### <----- ( this part is flat now! )
PS:附帶說明一下,模擬需要幾天時間才能完成,而且並非所有模型中的參數數量都是恆定的; 這就是為什么我想扁平化列表beta
,不管它里面是什么。 歡迎打包解決方案(例如, data.table
或dplyr
)。 謝謝。
我不確定這是否適用於真實數據,但它似乎產生了對上面示例數據的渴望。
map_depth(res, 2, flatten)
這是帶有str
的完整 output :
library(purrr)
map_depth(res, 2, flatten) %>% str
#> List of 4
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 0.71
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.485
#> .. ..$ metric2 : num 0.258
#> .. ..$ b1 : num 0.575
#> .. ..$ b2 : num 0.745
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 0.058
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.401
#> .. ..$ metric2 : num 0.257
#> .. ..$ b1 : num 0.535
#> .. ..$ b2 : num 0.775
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 0.046
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.475
#> .. ..$ metric2 : num 0.299
#> .. ..$ b1 : num 0.541
#> .. ..$ b2 : num 0.702
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 0.0474
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.241
#> .. ..$ metric2 : num 0.215
#> .. ..$ b1 : num 0.545
#> .. ..$ b2 : num 0.793
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 0.0502
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.713
#> .. ..$ metric2 : num 0.502
#> .. ..$ b1 : num 0.5
#> .. ..$ b2 : num 0.722
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 2.94
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.425
#> .. ..$ metric2 : num 0.282
#> .. ..$ b1 : num 0.555
#> .. ..$ b2 : num 0.712
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 0.041
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.671
#> .. ..$ metric2 : num 0.409
#> .. ..$ b1 : num 0.578
#> .. ..$ b2 : num 0.701
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: num 0.0396
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.459
#> .. ..$ metric2 : num 0.411
#> .. ..$ b1 : num 0.501
#> .. ..$ b2 : num 0.777
由代表 package (v0.3.0) 於 2021 年 2 月 21 日創建
更新
上述方法的問題是所有其他變量都轉換為numeric
,這是不可取的,因為time_iter
最初是一個difftime
object。
下面的方法更詳細,但不會將其他變量轉換為numeric
:
res %>%
map(
~ modify(.x, function(x) {
x <- append(x, unlist(x$beta))
x$beta <- NULL
x
})
下面是str
的 output :
res %>%
map(
~ modify(.x, function(x) {
x <- append(x, unlist(x$beta))
x$beta <- NULL
x
})
) %>% str
#> List of 4
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 0.7099
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.485
#> .. ..$ metric2 : num 0.258
#> .. ..$ b1 : num 0.575
#> .. ..$ b2 : num 0.745
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 0.058
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.401
#> .. ..$ metric2 : num 0.257
#> .. ..$ b1 : num 0.535
#> .. ..$ b2 : num 0.775
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 0.046
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.475
#> .. ..$ metric2 : num 0.299
#> .. ..$ b1 : num 0.541
#> .. ..$ b2 : num 0.702
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 0.0474
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 0.5
#> .. ..$ metric1 : num 0.241
#> .. ..$ metric2 : num 0.215
#> .. ..$ b1 : num 0.545
#> .. ..$ b2 : num 0.793
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 0.0502
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.713
#> .. ..$ metric2 : num 0.502
#> .. ..$ b1 : num 0.5
#> .. ..$ b2 : num 0.722
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 2.9419
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.5
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.425
#> .. ..$ metric2 : num 0.282
#> .. ..$ b1 : num 0.555
#> .. ..$ b2 : num 0.712
#> $ :List of 2
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 0.041
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 1
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.671
#> .. ..$ metric2 : num 0.409
#> .. ..$ b1 : num 0.578
#> .. ..$ b2 : num 0.701
#> ..$ :List of 10
#> .. ..$ modeltype: chr "tree"
#> .. ..$ time_iter: 'difftime' num 0.0396
#> .. .. ..- attr(*, "units")= chr "secs"
#> .. ..$ seed : num 2
#> .. ..$ nobs : num 75
#> .. ..$ hyperpar1: num 0.8
#> .. ..$ hyperpar2: num 1
#> .. ..$ metric1 : num 0.459
#> .. ..$ metric2 : num 0.411
#> .. ..$ b1 : num 0.501
#> .. ..$ b2 : num 0.777
由代表 package (v0.3.0) 於 2021 年 3 月 27 日創建
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.