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

Viewed 43

I want to do something like the following in 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? 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.

1 Answers

With dplyr::lst, we can do this

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

-output

$x
[1] 9

$y
[1] 17
Related