【问题标题】:Update lists elements programmatically looping through the lists names以编程方式循环遍历列表名称更新列表元素
【发布时间】:2022-11-04 00:06:08
【问题描述】:

我需要以编程方式循环遍历 R 中的列表名称来更新列表元素。

有没有办法使用某种非标准评估或其他方法来做到这一点?

最小示例的伪代码:

  x = list(a=1)
  y = list(a=3)
  
  for(i in c("x", "y")){
    
    i[["a"]] <- 10
  }

预期成绩:

  x[["a"]] == 10
  y[["a"]] == 10

【问题讨论】:

    标签: r non-standard-evaluation


    【解决方案1】:

    "x""y" 是字符串,我们需要 get 的值以及 assign

    for(i in c("x", "y")) {
        tmp <- get(i)
        tmp[["a"]] <- 10
        assign(i, tmp)
    }
    

    -检查

    > x
    $a
    [1] 10
    
    > y
    $a
    [1] 10
    

    【讨论】:

      猜你喜欢
      • 2018-07-11
      • 2018-09-05
      • 1970-01-01
      • 2019-06-25
      • 1970-01-01
      • 2021-03-06
      • 2017-06-03
      • 2017-02-08
      • 2013-03-04
      相关资源
      最近更新 更多