當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


R purrr list_simplify 將列表簡化為原子或 S3 向量

簡化維護輸入和輸出之間的一一對應關係,這意味著x 的每個元素必須包含一個單元素向量或one-row 數據幀。如果您不想保持這種對應關係,那麽您可能需要 list_c() /list_rbind()list_flatten()

用法

list_simplify(x, ..., strict = TRUE, ptype = NULL)

參數

x

一個列表。

...

這些點用於將來的擴展,並且必須為空。

strict

如果簡化失敗會發生什麽?如果是 TRUE ,則會出錯。如果未提供FALSEptype,則將原樣返回x

ptype

一個可選的原型,以確保輸出類型始終相同。

x 長度相同的向量。

例子

list_simplify(list(1, 2, 3))
#> [1] 1 2 3

# Only works when vectors are length one and have compatible types:
try(list_simplify(list(1, 2, 1:3)))
#> Error in list_simplify(list(1, 2, 1:3)) : 
#>   `x[[3]]` must have size 1, not size 3.
try(list_simplify(list(1, 2, "x")))
#> Error in list_simplify(list(1, 2, "x")) : 
#>   Can't combine `<list>[[1]]` <double> and `<list>[[3]]` <character>.

# Unless you strict = FALSE, in which case you get the input back:
list_simplify(list(1, 2, 1:3), strict = FALSE)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] 1 2 3
#> 
list_simplify(list(1, 2, "x"), strict = FALSE)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
#> [[3]]
#> [1] "x"
#> 
源代碼:R/list-simplify.R

相關用法


注:本文由純淨天空篩選整理自Hadley Wickham等大神的英文原創作品 Simplify a list to an atomic or S3 vector。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。