如何用purrr替换修改for循环

时间:2019-07-23 07:46:35

标签: r purrr

我想用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循环。我尝试过类似的操作,但是map总是返回一个新列表,而不只是更新它

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上出错了吗?

2 个答案:

答案 0 :(得分:2)

鉴于要修改的列表组件位于第二个嵌套级别,您还可以使用modify_depth直接访问它们:

library(purrr)

my_list <- modify_depth(my_list, 2, ~list_modify(.x, aaa = 99))

str(my_list)
#> 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 3
#>   .. ..$ aaa: num 99
#>   .. ..$ 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

NB:这也适用于第一个列表级别的多个列表组件(例如$a$b,...),而无需添加其他迭代步骤。


编辑:如modify_depth文档中列出:

  

modify_depth(x,2,fun)等效于x <-Modify(x,〜Modify(。,fun))

为了仅替换第二个嵌套级别上的一部分列表组件,我们可以用更详细的方式将对modify_depth的调用重写为:

library(purrr)

## replace aaa only for components 1 and 3
modify_loc <- c(TRUE, FALSE, TRUE)

my_list <- modify(my_list, ~imodify(., ~if(modify_loc[.y]) list_modify(.x, aaa = 99) else .x))

str(my_list)
#> 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 3
#>   .. ..$ aaa: int [1:3] 10 11 12
#>   .. ..$ 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

答案 1 :(得分:1)

一种方法是使用map并更改每个列表中的单个元素。

my_list[["a"]][x] <- purrr::map(my_list[["a"]][x], ~{.[["aaa"]] <- 99;.})


my_list
#$a
#$a[[1]]
#$a[[1]]$aaa
#[1] 99

#$a[[1]]$aab
#[1] 4 5 6

#$a[[1]]$aac
#[1] 7 8 9


#$a[[2]]
#$a[[2]]$aaa
#[1] 99

#$a[[2]]$aab
#[1] 13 14 15

#$a[[2]]$aac
#[1] 16 17 18
#......

与在基数R中使用lapply相同

my_list[["a"]][x] <- lapply(my_list[["a"]][x], function(x) {x[["aaa"]] <- 99;x})