Skip to content
Prev 61438 / 63421 Next

Multiple Assignment built into the R Interpreter?

I think the standard way to do this in R is given by list2env(), as 
described in a couple of answers on the SO page you linked.

The syntax you proposed would be likely to be confusing in complex 
expressions, e.g.

   f(A, C, Q, R = init_matrices(X, Y, Z))

would obviously not work but wouldn't trigger a syntax error, and

   f((A, C, Q, R = init_matrices(X, Y, Z)))

could work, but looks too much like the previous one.  So I think R 
would want Javascript-like

   [A, C, Q, R] <- init_matrices(X, Y, Z)

instead.  But then the question would come up about how to handle the 
RHS.  Does the function have to return a list?  What if the length of 
the list is not 4?  Or is it just guaranteed to be equivalent to

   temp <- init_matrices(X, Y, Z)
   A <- temp[[1]]
   C <- temp[[2]]
   Q <- temp[[3]]
   R <- temp[[4]]

which would work for other vector types besides lists?

BTW, here's a little hack that almost works:

`vals<-` <- function(x, ..., value) {
    others <- substitute(list(...))
    if (length(others) > 1)
      for (i in seq_along(others)[-1])
        assign(as.character(others[[i]]), value[[i]], envir = 
parent.frame())
    value[[1]]
}

You call it as

  vals(a, b, c) <- 1:3

and it assigns 1 to a, 2 to b, and 3 to c.  It doesn't quite do what you 
want because it requires that a exists already, but b and c don't have to.

Duncan Murdoch
On 11/03/2023 4:04 a.m., Sebastian Martin Krantz wrote:

Thread (22 messages)

Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Ivan Krylov Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 11 Gabriel Becker Multiple Assignment built into the R Interpreter? Mar 11 Kevin Ushey Multiple Assignment built into the R Interpreter? Mar 11 @vi@e@gross m@iii@g oii gm@ii@com Multiple Assignment built into the R Interpreter? Mar 11 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 11 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 12 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 12 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 12 Sebastian Martin Krantz Multiple Assignment built into the R Interpreter? Mar 12 Pavel Krivitsky Multiple Assignment built into the R Interpreter? Mar 12 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 13 Gabor Grothendieck Multiple Assignment built into the R Interpreter? Mar 13 Martin Maechler Multiple Assignment built into the R Interpreter? Mar 14 Duncan Murdoch Multiple Assignment built into the R Interpreter? Mar 14