【发布时间】:2020-04-08 17:18:44
【问题描述】:
使用基础 R,我想在嵌套列表上使用 mapply 函数。例如,在下面的代码中,我试图从嵌套列表的每个元素中删除字母“a”。我想用一行代码替换最后两行。
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)
【问题讨论】: