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


R purrr as_vector 將列表強製轉換為向量


[Superseded]

這些函數在 purrr 1.0.0 中被 list_simplify() 取代,它基於 vctr 原則具有更一致的語義:

  • as_vector(x) 現在是list_simplify(x)

  • simplify(x) 現在是list_simplify(x, strict = FALSE)

  • simplify_all(x)map(x, list_simplify, strict = FALSE)

被取代的函數不會消失,但隻會收到關鍵的錯誤修複。

用法

as_vector(.x, .type = NULL)

simplify(.x, .type = NULL)

simplify_all(.x, .type = NULL)

參數

.x

向量列表

.type

可以是指定要連接的向量的類型和長度的向量模具,例如 numeric(1)integer(4) 。或者,它可以是說明類型的字符串,其中之一:"logical"、"integer"、"double"、"complex"、"character" 或 "raw"。

例子

# was
as.list(letters) |> as_vector("character")
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
#> [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"
# now
as.list(letters) |> list_simplify(ptype = character())
#>  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q"
#> [18] "r" "s" "t" "u" "v" "w" "x" "y" "z"

# was:
list(1:2, 3:4, 5:6) |> as_vector(integer(2))
#> [1] 1 2 3 4 5 6
# now:
list(1:2, 3:4, 5:6) |> list_c(ptype = integer())
#> [1] 1 2 3 4 5 6

相關用法


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