1

After looking at this answer, I was confident that relist() should work to reconstruct a list in the following example:

a <- c("AA01_01", "AA01_03", "AA01_04", "AA01_06", "AA01_08", "AA01_11", "AA01_12", "AA01_13",
    "AA01_14", "AA01_16", "AA01_19", "AA01_20", "AA02_03", "AA02_04", "AA02_05", "AA02_06", "AA02_07",
    "AA02_08", "AA02_09", "AA02_13", "AA02_17", "AA02_19", "AA02_20", "AA03_05", "AA03_09", "AA03_10",
    "AA03_12", "AA03_16", "AA03_20", "AA04_01", "AA04_02", "AA04_03", "AA04_10", "AA04_11", "AA04_14",
    "AA04_16"
)

b <- list(
    b1 = c("AA01_01", "AA01_02", "AA01_03", "AA01_04", "AA01_05", "AA01_06", "AA01_07", "AA01_08", "AA01_09", "AA01_10",
        "AA01_11", "AA01_12", "AA01_13", "AA01_14", "AA01_15", "AA01_16", "AA01_17", "AA01_18", "AA01_19", "AA01_20"),
    b2 = c("AA02_01", "AA02_02", "AA02_03", "AA02_04", "AA02_05", "AA02_06", "AA02_07", "AA02_08", "AA02_09", "AA02_10",
        "AA02_11", "AA02_12", "AA02_13", "AA02_14", "AA02_15", "AA02_16", "AA02_17", "AA02_18", "AA02_19", "AA02_20"),
    b3 = c("AA03_01", "AA03_02", "AA03_03", "AA03_04", "AA03_05", "AA03_06", "AA03_07", "AA03_08", "AA03_09", "AA03_10",
        "AA03_11", "AA03_12", "AA03_13", "AA03_14", "AA03_15", "AA03_16", "AA03_17", "AA03_18", "AA03_19", "AA03_20"),
    b4 = c("AA04_01", "AA04_02", "AA04_03", "AA04_04", "AA04_05", "AA04_06", "AA04_07", "AA04_08", "AA04_09", "AA04_10",
        "AA04_11", "AA04_12", "AA04_13", "AA04_14", "AA04_15", "AA04_16", "AA04_17", "AA04_18", "AA04_19", "AA04_20")
)

newList <- relist(flesh = a, skeleton = b)

According to ?relist(), this should result in newList$b1 containing all characters starting with AA01_, newList$b2 should contain AA02_, and so on... but what I get is:

$b1
 [1] "AA01_01" "AA01_03" "AA01_04" "AA01_06" "AA01_08" "AA01_11" "AA01_12" "AA01_13" "AA01_14" "AA01_16" "AA01_19" "AA01_20" "AA02_03" "AA02_04"
[15] "AA02_05" "AA02_06" "AA02_07" "AA02_08" "AA02_09" "AA02_13"

$b2
 [1] "AA02_17" "AA02_19" "AA02_20" "AA03_05" "AA03_09" "AA03_10" "AA03_12" "AA03_16" "AA03_20" "AA04_01" "AA04_02" "AA04_03" "AA04_10" "AA04_11"
[15] "AA04_14" "AA04_16" NA        NA        NA        NA       

$b3
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

$b4
 [1] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA

Why does this happen and how should I proceed to create a list from a vector based on a model-list?

EDIT: As mentioned by @OganM in the comments, relist() requires the same structure after the list has been altered. So i rephrase my question: How can I create a list from a vector based on a structure of another list? I would prefer a solution that also works for nested lists.

Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Comfort Eagle
  • 2,112
  • 2
  • 22
  • 44
  • a has less elements than b has so it cannot be relisted. the purpose of relist is to use it after `a = unlist(b)` so that you can modify a and re-create the structure of b later. If you remove elements from a, the same structure cannot be acquired again since you don't know how to break apart the list anymore. Here, you see that in conservatively tries to fill the first 2 elements and `NA`s the rest – OganM Aug 30 '19 at 22:30
  • I see, thanks. Then `relist()` was apparently not what I was looking for. Is there an alternative function that will accomplish what I'm looking for? – Comfort Eagle Aug 30 '19 at 22:36
  • this might be relevant: https://stackoverflow.com/questions/57448008/how-to-create-all-combinations-from-a-nested-list-while-preserving-the-structure – chinsoon12 Aug 30 '19 at 23:32
  • Are you trying to find elements in `b` which are common to `a` ? What is your expected output? – Ronak Shah Aug 31 '19 at 02:06

2 Answers2

2
lapply(b, function(x) a[a %in% x])
#$b1
# [1] "AA01_01" "AA01_03" "AA01_04" "AA01_06" "AA01_08" "AA01_11" "AA01_12" "AA01_13"
# [9] "AA01_14" "AA01_16" "AA01_19" "AA01_20"

#$b2
# [1] "AA02_03" "AA02_04" "AA02_05" "AA02_06" "AA02_07" "AA02_08" "AA02_09" "AA02_13"
# [9] "AA02_17" "AA02_19" "AA02_20"

#$b3
#[1] "AA03_05" "AA03_09" "AA03_10" "AA03_12" "AA03_16" "AA03_20"

#$b4
#[1] "AA04_01" "AA04_02" "AA04_03" "AA04_10" "AA04_11" "AA04_14" "AA04_16"

Recursion might be necessary for nested lists

# Recursive function
foo = function(l, vect) {
    for (i in seq_along(l)) {
        l[[i]] = if (class(l[[i]]) == "list") {
            Recall(l[[i]], vect)
        } else {
            vect[ vect %in% l[[i]] ]
        }
    }
    return(l)
}

#DATA (nested list)
a = c("a", "b", "c", "d", "e", "f")
b = list(b1 = c("a", "b", "g", "h"),
         b2 = list(b21 = c("a", "d", "y"),
                   b22 = "f"))

# Usage
foo(b, a)
#> $b1
#> [1] "a" "b"
#> 
#> $b2
#> $b2$b21
#> [1] "a" "d"
#> 
#> $b2$b22
#> [1] "f"

Created on 2019-09-02 by the reprex package (v0.3.0)

d.b
  • 32,245
  • 6
  • 36
  • 77
0

If you are trying to find elements in a which are present in b you could use intersect

lapply(b, intersect, a)


#$b1
# [1] "AA01_01" "AA01_03" "AA01_04" "AA01_06" "AA01_08" "AA01_11" "AA01_12" 
#     "AA01_13" "AA01_14" "AA01_16" "AA01_19" "AA01_20"

#$b2
# [1] "AA02_03" "AA02_04" "AA02_05" "AA02_06" "AA02_07" "AA02_08" "AA02_09"  
#     "AA02_13" "AA02_17" "AA02_19" "AA02_20"

#$b3
#[1] "AA03_05" "AA03_09" "AA03_10" "AA03_12" "AA03_16" "AA03_20"

#$b4
#[1] "AA04_01" "AA04_02" "AA04_03" "AA04_10" "AA04_11" "AA04_14" "AA04_16"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213