我想用 purrr 替代方案替换简单的 for 循环。我该如何编码并保持对象的原始结构
这是我的例子:
my_list <- list(
a = list(
list(
aaa = c(1:3),
aab = c(4:6),
aac = c(7:9)),
list(
aaa = c(10:12),
aab = c(13:15),
aac = c(16:18)),
list(
aaa = c(19:21),
aab = c(22:24),
aac = c(25:27))
))
和我原来的解决方案
x <- purrr::map_lgl(my_list$a, .f = ~ !is.null(.x$aaa))
my_list1 <- my_list
for (i in which(x)) {
my_list1$a[[i]]$aaa <- 99
}
str(my_list1)
但是,我也想用 purrr 解决方案替换我的 for 循环。我尝试了类似的方法,但地图总是返回一个新列表,而不是仅仅更新它
my_list2 <- map(c(1, 3),
function(x){
my_list[["a"]][[x]][["aab"]] <- 99
return(my_list)
}
)
str(my_list2)
修改应该更适用于这种情况,但我也无法使其工作。
my_list3 <- purrr::map(
.x = which(x),
.f = function(i = .x) {
purrr::modify_in(.x = my_list,
.where = list("a", i, "aaa"),
.f = ~99)
}
)
str(my_list3)
新列表的结构应与初始列表相同:
# str(my_list)
List of 1
$ a:List of 3
..$ :List of 3
.. ..$ aaa: int [1:3] 1 2 3
.. ..$ aab: int [1:3] 4 5 6
.. ..$ aac: int [1:3] 7 8 9
..$ :List of 2
.. ..$ aab: int [1:3] 13 14 15
.. ..$ aac: int [1:3] 16 17 18
..$ :List of 3
.. ..$ aaa: int [1:3] 19 20 21
.. ..$ aab: int [1:3] 22 23 24
.. ..$ aac: int [1:3] 25 26 27
# str(my_list1) is correct
List of 1
$ a:List of 3
..$ :List of 3
.. ..$ aaa: num 99
.. ..$ aab: int [1:3] 4 5 6
.. ..$ aac: int [1:3] 7 8 9
..$ :List of 2
.. ..$ aab: int [1:3] 13 14 15
.. ..$ aac: int [1:3] 16 17 18
..$ :List of 3
.. ..$ aaa: num 99
.. ..$ aab: int [1:3] 22 23 24
.. ..$ aac: int [1:3] 25 26 27
# str(my_list2) or str (my_list3) is not correct
List of 2
$ :List of 1
..$ a:List of 3
.. ..$ :List of 3
.. .. ..$ aaa: num 99
.. .. ..$ aab: int [1:3] 4 5 6
.. .. ..$ aac: int [1:3] 7 8 9
.. ..$ :List of 2
.. .. ..$ aab: int [1:3] 13 14 15
.. .. ..$ aac: int [1:3] 16 17 18
.. ..$ :List of 3
.. .. ..$ aaa: int [1:3] 19 20 21
.. .. ..$ aab: int [1:3] 22 23 24
.. .. ..$ aac: int [1:3] 25 26 27
$ :List of 1
..$ a:List of 3
.. ..$ :List of 3
.. .. ..$ aaa: int [1:3] 1 2 3
.. .. ..$ aab: int [1:3] 4 5 6
.. .. ..$ aac: int [1:3] 7 8 9
.. ..$ :List of 2
.. .. ..$ aab: int [1:3] 13 14 15
.. .. ..$ aac: int [1:3] 16 17 18
.. ..$ :List of 3
.. .. ..$ aaa: num 99
.. .. ..$ aab: int [1:3] 22 23 24
.. .. ..$ aac: int [1:3] 25 26 27
有什么提示我的 purrr 出错了吗?