Skip to content
Prev 299559 / 398503 Next

returning multiple values

Hi,

No, you won't be able to simply call "a" and have that work.

R returns these in a single object with components (elements) named a,b,c,d

Here's a concrete example:

func <- function(x, y) return(list(a = x+1, b = y + 2))

out <- func(3, 5)

out[["a"]] # or out$a

out[["b"]] # or out$b

give the desired results.

If you just do

func(3,5)

the value will be returned, but since it's not bound to a variable
name, simply thrown away.

Remember -- R is (almost) always pass-by-value, not by reference, and
has pretty strong scoping.

Best,
Michael
On Mon, Jul 9, 2012 at 1:02 PM, PRAGYA SUR <pragya1386 at gmail.com> wrote: