我正在嘗試重命名一個串列物件,其中物件名稱與 str_c() 或 paste() 放在一起,但似乎無法讓它作業。我認為這是可能的,但我不知道如何。
# some data
params <- tibble(col1=c(1,2,3), col2=c("a", "b", "c"))
# create empty list
all_output <- list()
# append new df to list
all_output <- append(all_output, list("this_works_fine" = params))
# append new df to list using the concatenated string
all_output <- append(all_output, list(str_c("this_", "does_", "not_", "work") = params))
謝謝你的幫助!
uj5u.com熱心網友回復:
使用setNames或
append(all_output, setNames(list(params), str_c("this_", "does_", "not_", "work")))
-輸出
$this_works_fine
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c
$this_does_not_work
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c
或使用lstfrom dplyrwith:=
append(all_output, lst(!!str_c("this_", "does_", "not_", "work") := params))
-輸出
$this_works_fine
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c
$this_does_not_work
# A tibble: 3 × 2
col1 col2
<dbl> <chr>
1 1 a
2 2 b
3 3 c
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/324579.html
上一篇:Ggplotboxplot按組,更改顯示的匯總統計資訊
下一篇:串列中所有可能的組合
