简体   繁体   English

使用一个列表项作为 R 中同一列表中另一个列表项的变量

[英]Use one list item as a variable in another list item in the same list in R

I want to do something like the following in R:我想在 R 中做类似下面的事情:

a <- list(
  x = 2 + 7,
  y = x + 8
)

It appears that this is not possible, though, or at least not in this way.然而,这似乎是不可能的,或者至少不是这样。 Is there any way to use the value of x in defining y ?有没有办法在定义y使用x的值? I know I could define two objects independently, x and y , then do list(x, y) , but I'd rather skip creating a lot of extra objects in my workspace.我知道我可以独立定义两个对象, xy ,然后做list(x, y) ,但我宁愿跳过在我的工作区中创建很多额外的对象。

With dplyr::lst , we can do this使用dplyr::lst ,我们可以做到这一点

library(dplyr)
lst(x = 2 + 7, y = x + 8)

-output -输出

$x
[1] 9

$y
[1] 17

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM