[英]Given a named list of character vectors, output a new list where the unique elements of all character vectors reverse place with names of origin list
這是一個簡化的例子。 我的真實數據集非常大,所以答案在計算上應該是便宜的。
start.list <- list(cb_1 = c("VR_1", "VR_2", "VR_3"), cb_2 = c("VR_2", "VR_3", "VR_4"),
cb_3 = c("VR_3", "VR_4", "VR_5"))
> start.list
$cb_1
[1] "VR_1" "VR_2" "VR_3"
$cb_2
[1] "VR_2" "VR_3" "VR_4"
$cb_3
[1] "VR_3" "VR_4" "VR_5"
所需的 output 列表:
> output.list
$VR_1
[1] "cb_1"
$VR_2
[1] "cb_1" "cb_2"
$VR_3
[1] "cb_1" "cb_2" "cb_3"
$VR_4
[1] "cb_2" "cb_3"
$VR_5
[1] "cb_3"
使用enframe
和unnest
-
tibble::enframe(start.list) %>%
tidyr::unnest(value) %>%
{split(.$name, .$value)}
#$VR_1
#[1] "cb_1"
#$VR_2
#[1] "cb_1" "cb_2"
#$VR_3
#[1] "cb_1" "cb_2" "cb_3"
#$VR_4
#[1] "cb_2" "cb_3"
#$VR_5
#[1] "cb_3"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.