修改指定的列表元素 [英] Modifying specified list elements r

查看:59
本文介绍了修改指定的列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输出看起来像这样:

I have an output that looks like this:

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)))

我想选择和修改子列表x和y的特定元素.例如,我想用向量或函数生成的数字替换x的最后一个元素-即;将向量 z<-c(10,50,6,12)映射到子列表x的最后一个元素,得到:

I would like to select and modify specific elements of sublists x and y. For example I would like to replace the last element of x with a number from a vector or generated from a function - i.e; map the vector z <- c(10, 50, 6, 12) over the last element of sublists x to result in:

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)))

或在每个子列表x的最后一个元素上使用诸如 rnorm(1)之类的函数,以产生来自以下内容的输出:

or a function such as rnorm(1) over the last element of each sublist x to result in the output from:

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软件包,但是语法对我来说是新的.

I have been trying the purrr package but the syntax are new to me.

尝试:

purrr :: map(test_list,〜.x $ x [length(.x $ x)])选择正确的列表/深度,但无法修改.

purrr::map(test_list, ~ .x$x[length(.x$x)]) selects correct list/depth but unable to modify.

purrr :: modify_depth(test_list,2,rnorm(1))我可以看到为什么它选择了不正确的深度但不确定如何纠正.

purrr::modify_depth(test_list, 2, rnorm(1)) I can see why this selects the incorrect depth but not sure how to correct.

到目前为止,我只能找到以简单方式操作简单列表结构的示例,或者我没有设法转移的复杂示例.

So far I can only find examples of manipulating simple list structures in simple ways or complex examples I have not managed to transfer.

任何帮助将不胜感激,总的来说,我的尝试似乎很笨拙,很难一概而论,以备将来使用.

Any help would be much appreciated, overall my attempts seem quite clumsy and difficult to generalise for future use.

谢谢!

推荐答案

在基本R中,您可以使用 Map .

In base R you can use 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; })

这篇关于修改指定的列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆