修改指定列表元素 r
Modifying specified list elements r
我的输出如下所示:
test_list <- list(list(x = c(10, 101, 3), y = c(9, 12, 11)),
list(x = c(10, 133, 4), y = c(9, 15, 13)),
list(x = c(10, 101, 90), y = c(9, 18, 11)),
list(x = c(10, 101, 1), y = c(9, 10, 15)))
我想 select 并修改子列表 x 和 y 的特定元素。例如,我想用向量中的数字或函数生成的数字替换 x 的最后一个元素 - 即;将向量 z <- c(10, 50, 6, 12)
映射到子列表 x 的最后一个元素,结果为:
test_list_1 <- list(list(x = c(10, 101, 10), y = c(9, 12, 11)),
list(x = c(10, 133, 50), y = c(9, 15, 13)),
list(x = c(10, 101, 6), y = c(9, 18, 11)),
list(x = c(10, 101, 12), y = c(9, 10, 15)))
或诸如 rnorm(1)
之类的函数遍历每个子列表 x 的最后一个元素,以产生以下输出:
test_list_2 <- list(list(x = c(10, 101, rnorm(1)), y = c(9, 12, 11)),
list(x = c(10, 133, rnorm(1)), y = c(9, 15, 13)),
list(x = c(10, 101, rnorm(1)), y = c(9, 18, 11)),
list(x = c(10, 101, rnorm(1)), y = c(9, 10, 15)))
我一直在尝试 purrr 包,但语法对我来说是新的。
已尝试:
purrr::map(test_list, ~ .x$x[length(.x$x)])
select正确list/depth但无法修改。
purrr::modify_depth(test_list, 2, rnorm(1))
我明白为什么 select 深度不正确,但不确定如何更正。
到目前为止我只能找到以简单的方式操作简单列表结构的例子或我没有设法转移的复杂例子。
任何帮助将不胜感激,总的来说我的尝试看起来很笨拙并且难以概括以供将来使用。
谢谢!
在基础 R 中你可以使用 Map
。
示例 1
z <- c(10, 50, 6, 12)
Map(function(v, w) { v$x <- replace(v$x, 3, w); v; }, test_list, z)
#[[1]]
#[[1]]$x
#[1] 10 101 10
#
#[[1]]$y
#[1] 9 12 11
#
#
#[[2]]
#[[2]]$x
#[1] 10 133 50
#
#[[2]]$y
#[1] 9 15 13
#
#
#[[3]]
#[[3]]$x
#[1] 10 101 6
#
#[[3]]$y
#[1] 9 18 11
#
#
#[[4]]
#[[4]]$x
#[1] 10 101 12
#
#[[4]]$y
#[1] 9 10 15
示例 2
Map(function(v, w) { v$x <- replace(v$x, 3, w); v; }, test_list, rnorm(length(test_list)))
或使用 purrr::map2
相同
purrr::map2(test_list, z, function(v, w) { v$x <- replace(v$x, 3, w); v; })
purrr::map2(test_list, rnorm(length(test_list)), function(v, w) { v$x <- replace(v$x, 3, w); v; })
我的输出如下所示:
test_list <- list(list(x = c(10, 101, 3), y = c(9, 12, 11)),
list(x = c(10, 133, 4), y = c(9, 15, 13)),
list(x = c(10, 101, 90), y = c(9, 18, 11)),
list(x = c(10, 101, 1), y = c(9, 10, 15)))
我想 select 并修改子列表 x 和 y 的特定元素。例如,我想用向量中的数字或函数生成的数字替换 x 的最后一个元素 - 即;将向量 z <- c(10, 50, 6, 12)
映射到子列表 x 的最后一个元素,结果为:
test_list_1 <- list(list(x = c(10, 101, 10), y = c(9, 12, 11)),
list(x = c(10, 133, 50), y = c(9, 15, 13)),
list(x = c(10, 101, 6), y = c(9, 18, 11)),
list(x = c(10, 101, 12), y = c(9, 10, 15)))
或诸如 rnorm(1)
之类的函数遍历每个子列表 x 的最后一个元素,以产生以下输出:
test_list_2 <- list(list(x = c(10, 101, rnorm(1)), y = c(9, 12, 11)),
list(x = c(10, 133, rnorm(1)), y = c(9, 15, 13)),
list(x = c(10, 101, rnorm(1)), y = c(9, 18, 11)),
list(x = c(10, 101, rnorm(1)), y = c(9, 10, 15)))
我一直在尝试 purrr 包,但语法对我来说是新的。
已尝试:
purrr::map(test_list, ~ .x$x[length(.x$x)])
select正确list/depth但无法修改。
purrr::modify_depth(test_list, 2, rnorm(1))
我明白为什么 select 深度不正确,但不确定如何更正。
到目前为止我只能找到以简单的方式操作简单列表结构的例子或我没有设法转移的复杂例子。
任何帮助将不胜感激,总的来说我的尝试看起来很笨拙并且难以概括以供将来使用。
谢谢!
在基础 R 中你可以使用 Map
。
示例 1
z <- c(10, 50, 6, 12)
Map(function(v, w) { v$x <- replace(v$x, 3, w); v; }, test_list, z)
#[[1]]
#[[1]]$x
#[1] 10 101 10
#
#[[1]]$y
#[1] 9 12 11
#
#
#[[2]]
#[[2]]$x
#[1] 10 133 50
#
#[[2]]$y
#[1] 9 15 13
#
#
#[[3]]
#[[3]]$x
#[1] 10 101 6
#
#[[3]]$y
#[1] 9 18 11
#
#
#[[4]]
#[[4]]$x
#[1] 10 101 12
#
#[[4]]$y
#[1] 9 10 15
示例 2
Map(function(v, w) { v$x <- replace(v$x, 3, w); v; }, test_list, rnorm(length(test_list)))
或使用 purrr::map2
purrr::map2(test_list, z, function(v, w) { v$x <- replace(v$x, 3, w); v; })
purrr::map2(test_list, rnorm(length(test_list)), function(v, w) { v$x <- replace(v$x, 3, w); v; })