是否可以在R中一步来复制和修改列表? [英] Is it possible to duplicate and modify a list in one step in R?
本文介绍了是否可以在R中一步来复制和修改列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我发现自己经常这样做:
I find myself doing this a lot:
my_list <- list(a = list(b = "something"), c = 3, d = "a lot of other options")
my_list_2 <- my_list
my_list_2$a$b <- "something_else"
my_list_2$c <- 5
我想知道是否有速记.像这样:
I was wondering if there was a shorthand for this. Something like:
my_list_2 <- xxxxx(my_list, list(a = list(b = "something_else"), c = 5))
更新:我的预期输出是:
my_list # unchanged
my_list_2 # list(a = list(b = "something_else"), c = 5, d = "a lot of other options")
推荐答案
一种选择是使用?within
:
within(my_list, {a$b = "something_else"; c = 5})
#$a
#$a$b
#[1] "something_else"
#
#
#$c
#[1] 5
#
#$d
#[1] "a lot of other options"
当然可以通过将结果分配给新对象(例如
Of course the duplication is possible in the same step by assigning the result to a new object, e.g.
my_list2 <- within(my_list, {a$b = "something_else"; c = 5})
这篇关于是否可以在R中一步来复制和修改列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文