R use mapply on nested list
Asked Answered
A

3

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)
Appalling answered 8/4, 2020 at 17:18 Comment(0)
E
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)
Eduard answered 8/4, 2020 at 17:24 Comment(0)
C
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"
Casern answered 8/4, 2020 at 17:27 Comment(0)
P
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')
Pauiie answered 8/4, 2020 at 17:26 Comment(0)
E
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)
Eduard answered 8/4, 2020 at 17:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.