Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R use mapply on nested list

Tags:

r

nested

mapply

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)
like image 678
user1491868 Avatar asked Apr 08 '20 17:04

user1491868


3 Answers

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"
like image 129
tmfmnk Avatar answered Sep 23 '22 14:09

tmfmnk


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')
like image 25
akrun Avatar answered Sep 23 '22 14:09

akrun


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)
like image 42
Rui Barradas Avatar answered Sep 23 '22 14:09

Rui Barradas