gpt4 book ai didi

r - 如何在R中使用部分不同的键合并列表?

转载 作者:行者123 更新时间:2023-12-04 10:11:52 25 4
gpt4 key购买 nike

这应该很容易,但是我发现的所有示例都有稍微不同的目标。

我得到了 list :

lst1 = list(
Plot = TRUE,
Constrain = c(1:10),
Box = "plot"
)

lst2 = list(
Plot = FALSE,
Lib = "custom"
)

存储默认参数(lst1)和自定义参数(lst2)的参数,这些参数应覆盖默认值。结果我想要:
>lst
$Plot
[1] FALSE

$Constrain
[1] 1 2 3 4 5 6 7 8 9 10

$Box
[1] "plot"

$Lib
[1] "custom"

所以:

lst1中确实存在的lst2的
  • 参数将覆盖

  • lst2中不存在的lst1的
  • 参数将被保留
  • lst1中不存在的lst2的
  • 参数将被添加

  • 对不起,我无法弄清楚。我尝试过merge():
    lst=merge(lst2,lst1)


    [1] Plot      Lib       Constrain Box      
    <0 Zeilen> (oder row.names mit Länge 0)

    - 编辑 -
    fabians建议的解决方案正是我所需要的。甚至更多:它可以处理嵌套列表,例如
    ParametersDefault = list(  
    Plot = list(
    Surface = TRUE,
    PlanView= TRUE
    ),
    Constrain = c(1:10),
    Box = "plot"
    )

    Parameters = list(
    Plot = list(
    Surface = FALSE,
    Env = TRUE
    ),
    Lib = "custom"
    )
    Parameters = modifyList(ParametersDefault,Parameters)

    print(Parameters$Plot$Surface)
    # [1] FALSE

    非常感谢!

    最佳答案

    lst1 = list(
    Plot = TRUE,
    Constrain = c(1:10),
    Box = "plot"
    )

    lst2 = list(
    Plot = FALSE,
    Lib = "custom"
    )

    modifyList(lst1, lst2)
    # $Plot
    # [1] FALSE
    #
    # $Constrain
    # [1] 1 2 3 4 5 6 7 8 9 10
    #
    # $Box
    # [1] "plot"
    #
    # $Lib
    # [1] "custom"

    关于r - 如何在R中使用部分不同的键合并列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22689082/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com