5

Using base R, I'd like to use the mapply function on a nested list. For example, in the code below, I'm trying to remove the letter "a" from each element of a nested list. I'd like to replace the last two lines with just a single line of code.

mylist <- list(
    list(c("a", "b", "c"), c("d", "e", "f")),
    list(c("a", "v", "w"), c("x", "y"), c("c", "b", "a"))
)

mylist

not_a <- lapply(mylist, lapply, `!=`, "a")

not_a

mylist[[1]] <- mapply(`[`, mylist[[1]], not_a[[1]], SIMPLIFY = FALSE)

mylist[[2]] <- mapply(`[`, mylist[[2]], not_a[[2]], SIMPLIFY = FALSE)
user1491868
  • 596
  • 4
  • 15
  • 42

3 Answers3

5

One option could be:

rapply(mylist, how = "replace", function(x) x[x != "a"])

[[1]]
[[1]][[1]]
[1] "b" "c"

[[1]][[2]]
[1] "d" "e" "f"


[[2]]
[[2]][[1]]
[1] "v" "w"

[[2]][[2]]
[1] "x" "y"

[[2]][[3]]
[1] "c" "b"
tmfmnk
  • 38,881
  • 4
  • 47
  • 67
3

Or using map2

library(purrr)
map2(mylist, not_a, ~ map2(.x, .y, `[`))

Or using map_depth (if the OP is interested only in the final outcome)

map_depth(mylist, 2,  ~ .x[.x != 'a'])
#[[1]]
#[[1]][[1]]
#[1] "b" "c"

#[[1]][[2]]
#[1] "d" "e" "f"


#[[2]]
#[[2]][[1]]
#[1] "v" "w"

#[[2]][[2]]
#[1] "x" "y"

#[[2]][[3]]
#[1] "c" "b"

Or more compactly

map_depth(mylist, 2, setdiff, 'a')
akrun
  • 874,273
  • 37
  • 540
  • 662
2

A double loop Map/mapply will do what the question asks for.

Map(function(i) mapply(`[`, mylist[[i]], not_a[[i]], SIMPLIFY = FALSE), seq_along(mylist))

Simpler:

Map(function(x, y) Map(`[`, x, y), mylist, not_a)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66