1

This is a simplified example. My real dataset is very large, so the answer should be computationally inexpensive.

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"

Desired output list:

> 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"
Max Hills
  • 11
  • 1

1 Answers1

0

Using enframe and 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"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213