簡體   English   中英

是否可以從另一個嵌套列表創建列表?

[英]Is it possible to create a list from another nested list?

我有一個處理了n個數據的列表,每個數據中有15個數組。 我必須為每種類型的數組創建一個列表,比如

list1 <- Dados_processados[[1]][[1]], Dados_processados[[2]][[1]], 
   Dados_processados[[3]][[1]]...Dados_processados[[n]][[1]]

這是數據的樣子

在此輸入圖像描述

我試圖用'while'做到這一點,但它出錯了。

為了使示例更小,我將n設為3 ,每個列表為長度5而不是15

l <- list(
  as.list(1:5),
  as.list(11:15),
  as.list(21:25)
)
str(l)
#> List of 3
#>  $ :List of 5
#>   ..$ : int 1
#>   ..$ : int 2
#>   ..$ : int 3
#>   ..$ : int 4
#>   ..$ : int 5
#>  $ :List of 5
#>   ..$ : int 11
#>   ..$ : int 12
#>   ..$ : int 13
#>   ..$ : int 14
#>   ..$ : int 15
#>  $ :List of 5
#>   ..$ : int 21
#>   ..$ : int 22
#>   ..$ : int 23
#>   ..$ : int 24
#>   ..$ : int 25

purrr::transpose將按照您描述的方式將列表“從里到外”轉換。

l2 <- purrr::transpose(l)
str(l2)
#> List of 5
#>  $ :List of 3
#>   ..$ : int 1
#>   ..$ : int 11
#>   ..$ : int 21
#>  $ :List of 3
#>   ..$ : int 2
#>   ..$ : int 12
#>   ..$ : int 22
#>  $ :List of 3
#>   ..$ : int 3
#>   ..$ : int 13
#>   ..$ : int 23
#>  $ :List of 3
#>   ..$ : int 4
#>   ..$ : int 14
#>   ..$ : int 24
#>  $ :List of 3
#>   ..$ : int 5
#>   ..$ : int 15
#>   ..$ : int 25

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM